最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解matplotlib中pyplot和面向?qū)ο髢煞N繪圖模式之間的關(guān)系

 更新時間:2021年01月22日 10:36:03   作者:mighty13  
這篇文章主要介紹了詳解matplotlib中pyplot和面向?qū)ο髢煞N繪圖模式之間的關(guān)系,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

matplotlib有兩種繪圖方式,一種是依托matplotlib.pyplot模塊實(shí)現(xiàn)類似matlab繪圖指令的繪圖方式,一種是面向?qū)ο笫嚼L圖,依靠FigureCanvas(畫布)、 Figure (圖像)、 Axes (軸域) 等對象繪圖。

這兩種方式之間并不是完全獨(dú)立的,而是通過某種機(jī)制進(jìn)行了聯(lián)結(jié),pylot繪圖模式其實(shí)隱式創(chuàng)建了面向?qū)ο竽J降南嚓P(guān)對象,其中的關(guān)鍵是matplotlib._pylab_helpers模塊中的單例類Gcf,它的作用是追蹤當(dāng)前活動的畫布及圖像。

因此,可以說matplotlib繪圖的基礎(chǔ)是面向?qū)ο笫嚼L圖,pylot繪圖模式只是一種簡便繪圖方式。

先不分析源碼,先做實(shí)驗(yàn)!

實(shí)驗(yàn)

先通過實(shí)驗(yàn),看一看我們常用的那些pyplot繪圖模式

實(shí)驗(yàn)一
無繪圖窗口顯示

from matplotlib import pyplot as plt
plt.show()

實(shí)驗(yàn)二
出現(xiàn)繪圖結(jié)果

from matplotlib import pyplot as plt
plt.plot([1,2])
plt.show()

實(shí)驗(yàn)三
出現(xiàn)繪圖結(jié)果

from matplotlib import pyplot as plt
plt.gca()
plt.show()

實(shí)驗(yàn)四
出現(xiàn)繪圖結(jié)果

from matplotlib import pyplot as plt
plt.figure()
# 或者plt.gcf()
plt.show()

pyplot模塊繪圖原理

通過查看pyplot模塊figure()函數(shù)、gcf()函數(shù)、gca()函數(shù)、plot()函數(shù)和其他繪圖函數(shù)的源碼,可以簡單理個思路!

  • figure()函數(shù):如果有現(xiàn)成圖像,返回值就是當(dāng)前圖像,如果沒有現(xiàn)成的圖像,就初始化一個新圖像,返回值為Figure對象。
  • gcf()函數(shù):如果有現(xiàn)成圖像,返回值就是當(dāng)前圖像,如果沒有現(xiàn)成的圖像,就調(diào)用figure()函數(shù),返回值為Figure對象。
  • gca()函數(shù):調(diào)用gcf()函數(shù)返回對象的gca方法,返回值為Axes對象。
  • plot()函數(shù):調(diào)用gca()函數(shù)返回對象的plot方法。
  • pyplot模塊其他繪圖函數(shù):均調(diào)用gca()函數(shù)的相關(guān)方法。

因此,pyplot繪圖模式,使用plot()函數(shù)或者其他繪圖函數(shù),如果沒有現(xiàn)成圖像對象,直接會先創(chuàng)建圖像對象。
當(dāng)然使用figure()函數(shù)、gcf()函數(shù)和gca()函數(shù),如果沒有現(xiàn)成圖像對象,也會先創(chuàng)建圖像對象。

更進(jìn)一步,在matplotlib.pyplot模塊源碼中出現(xiàn)了如下代碼,因此再查看matplotlib._pylab_helpers模塊它的作用是追蹤當(dāng)前活動的畫布及圖像

figManager = _pylab_helpers.Gcf.get_fig_manager(num)
figManager = _pylab_helpers.Gcf.get_active()

matplotlib._pylab_helpers模塊作用是管理pyplot繪圖模式中的圖像。該模塊只有一個類——Gcf,它的作用是追蹤當(dāng)前活動的畫布及圖像。

matplotlib.pyplot模塊部分源碼

def figure(num=None, # autoincrement if None, else integer from 1-N
      figsize=None, # defaults to rc figure.figsize
      dpi=None, # defaults to rc figure.dpi
      facecolor=None, # defaults to rc figure.facecolor
      edgecolor=None, # defaults to rc figure.edgecolor
      frameon=True,
      FigureClass=Figure,
      clear=False,
      **kwargs
      ):

  figManager = _pylab_helpers.Gcf.get_fig_manager(num)
  if figManager is None:
    max_open_warning = rcParams['figure.max_open_warning']

    if len(allnums) == max_open_warning >= 1:
      cbook._warn_external(
        "More than %d figures have been opened. Figures "
        "created through the pyplot interface "
        "(`matplotlib.pyplot.figure`) are retained until "
        "explicitly closed and may consume too much memory. "
        "(To control this warning, see the rcParam "
        "`figure.max_open_warning`)." %
        max_open_warning, RuntimeWarning)

    if get_backend().lower() == 'ps':
      dpi = 72

    figManager = new_figure_manager(num, figsize=figsize,
                    dpi=dpi,
                    facecolor=facecolor,
                    edgecolor=edgecolor,
                    frameon=frameon,
                    FigureClass=FigureClass,
                    **kwargs)
  return figManager.canvas.figure

def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
  return gca().plot(
    *args, scalex=scalex, scaley=scaley,
    **({"data": data} if data is not None else {}), **kwargs)

def gcf():
  """
  Get the current figure.

  If no current figure exists, a new one is created using
  `~.pyplot.figure()`.
  """
  figManager = _pylab_helpers.Gcf.get_active()
  if figManager is not None:
    return figManager.canvas.figure
  else:
    return figure()

def gca(**kwargs):
  return gcf().gca(**kwargs)

def get_current_fig_manager():
  """
  Return the figure manager of the current figure.

  The figure manager is a container for the actual backend-depended window
  that displays the figure on screen.

  If if no current figure exists, a new one is created an its figure
  manager is returned.

  Returns
  -------
  `.FigureManagerBase` or backend-dependent subclass thereof
  """
  return gcf().canvas.manager

Gcf類源碼

class Gcf:
  """
  Singleton to maintain the relation between figures and their managers, and
  keep track of and "active" figure and manager.

  The canvas of a figure created through pyplot is associated with a figure
  manager, which handles the interaction between the figure and the backend.
  pyplot keeps track of figure managers using an identifier, the "figure
  number" or "manager number" (which can actually be any hashable value);
  this number is available as the :attr:`number` attribute of the manager.

  This class is never instantiated; it consists of an `OrderedDict` mapping
  figure/manager numbers to managers, and a set of class methods that
  manipulate this `OrderedDict`.

  Attributes
  ----------
  figs : OrderedDict
    `OrderedDict` mapping numbers to managers; the active manager is at the
    end.
  """

到此這篇關(guān)于詳解matplotlib中pyplot和面向?qū)ο髢煞N繪圖模式之間的關(guān)系的文章就介紹到這了,更多相關(guān)matplotlib中pyplot和面向?qū)ο髢?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • cmd運(yùn)行python文件時對結(jié)果進(jìn)行保存的方法

    cmd運(yùn)行python文件時對結(jié)果進(jìn)行保存的方法

    今天小編就為大家分享一篇cmd運(yùn)行python文件時對結(jié)果進(jìn)行保存的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • python3+PyQt5+Qt Designer實(shí)現(xiàn)擴(kuò)展對話框

    python3+PyQt5+Qt Designer實(shí)現(xiàn)擴(kuò)展對話框

    這篇文章主要為大家詳細(xì)介紹了python3+PyQt5+Qt Designer實(shí)現(xiàn)擴(kuò)展對話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python使用getpass庫讀取密碼的示例

    Python使用getpass庫讀取密碼的示例

    本篇文章主要介紹了Python使用getpass庫讀取密碼的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 手把手教你在Python里使用ChatGPT

    手把手教你在Python里使用ChatGPT

    最近幾天我一直在玩?ChatGPT,我對使用這個工具的無限可能性著迷,下面這篇文章主要給大家介紹了關(guān)于在Python里使用ChatGPT的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • 詳細(xì)分析python3的reduce函數(shù)

    詳細(xì)分析python3的reduce函數(shù)

    小編給大家整理了python3的reduce函數(shù)詳細(xì)用法以及相關(guān)的技巧,需要的朋友們參考一下吧。
    2017-12-12
  • CentOS中升級Python版本的方法詳解

    CentOS中升級Python版本的方法詳解

    本文給大家分享的是再centos系統(tǒng)中將Python版本從2.6升級到2.7的方法和升級過程中遇到的問題的處理,非常詳細(xì),有需要的小伙伴可以參考下
    2017-07-07
  • Python中asyncore異步模塊的用法及實(shí)現(xiàn)httpclient的實(shí)例

    Python中asyncore異步模塊的用法及實(shí)現(xiàn)httpclient的實(shí)例

    asyncore即是一個異步的socket封裝,特別是dispatcher類中包含了很多異步調(diào)用的socket操作方法,非常犀利,下面我們就來講解Python中asyncore異步模塊的用法及實(shí)現(xiàn)httpclient的實(shí)例
    2016-06-06
  • Django之choices選項(xiàng)和富文本編輯器的使用詳解

    Django之choices選項(xiàng)和富文本編輯器的使用詳解

    這篇文章主要介紹了Django之choices選項(xiàng)和富文本編輯器的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python獲取文件ssdeep值的方法

    Python獲取文件ssdeep值的方法

    這篇文章主要介紹了Python獲取文件ssdeep值的方法,是一個比較實(shí)用的技巧,本文詳細(xì)講述了實(shí)現(xiàn)這一功能的具體步驟及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2014-10-10
  • python的exec、eval使用分析

    python的exec、eval使用分析

    這篇文章主要介紹了python的exec、eval使用分析,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12

最新評論

宜川县| 涡阳县| 集贤县| 惠东县| 西畴县| 遂宁市| 广宁县| 汤原县| 山阴县| 绍兴市| 丰镇市| 龙岩市| 北安市| 三穗县| 衡南县| 成安县| 南投市| 蒙自县| 海安县| 瑞丽市| 彭山县| 大石桥市| 怀安县| 华阴市| 石城县| 当雄县| 政和县| 中江县| 如皋市| 施甸县| 即墨市| 和顺县| 台州市| 江油市| 海盐县| 玛曲县| 南雄市| 贵阳市| 巍山| 宁化县| 巴塘县|