詳解matplotlib中pyplot和面向?qū)ο髢煞N繪圖模式之間的關(guān)系
matplotlib有兩種繪圖方式,一種是依托matplotlib.pyplot模塊實現(xiàn)類似matlab繪圖指令的繪圖方式,一種是面向?qū)ο笫嚼L圖,依靠FigureCanvas(畫布)、 Figure (圖像)、 Axes (軸域) 等對象繪圖。
這兩種方式之間并不是完全獨立的,而是通過某種機制進行了聯(lián)結(jié),pylot繪圖模式其實隱式創(chuàng)建了面向?qū)ο竽J降南嚓P(guān)對象,其中的關(guān)鍵是matplotlib._pylab_helpers模塊中的單例類Gcf,它的作用是追蹤當(dāng)前活動的畫布及圖像。
因此,可以說matplotlib繪圖的基礎(chǔ)是面向?qū)ο笫嚼L圖,pylot繪圖模式只是一種簡便繪圖方式。
先不分析源碼,先做實驗!
實驗
先通過實驗,看一看我們常用的那些pyplot繪圖模式
實驗一
無繪圖窗口顯示
from matplotlib import pyplot as plt plt.show()
實驗二
出現(xiàn)繪圖結(jié)果
from matplotlib import pyplot as plt plt.plot([1,2]) plt.show()
實驗三
出現(xiàn)繪圖結(jié)果
from matplotlib import pyplot as plt plt.gca() plt.show()
實驗四
出現(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)建圖像對象。
更進一步,在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)文章
總結(jié)python 三種常見的內(nèi)存泄漏場景
這篇文章主要介紹了總結(jié)python 三種常見的內(nèi)存泄漏場景,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11python GUI庫圖形界面開發(fā)之PyQt5訪問系統(tǒng)剪切板QClipboard類詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5訪問系統(tǒng)剪切板QClipboard類詳細使用方法與實例,需要的朋友可以參考下2020-02-02Python使用PyCrypto實現(xiàn)AES加密功能示例
這篇文章主要介紹了Python使用PyCrypto實現(xiàn)AES加密功能,結(jié)合具體實例形式分析了PyCrypto實現(xiàn)AES加密的操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-05-05Django1.7+python 2.78+pycharm配置mysql數(shù)據(jù)庫教程
原本感覺在Django1.7+python 2.78+pycharm環(huán)境下配置mysql數(shù)據(jù)庫是件很容易的事情,結(jié)果具體操作的時候才發(fā)現(xiàn),問題還是挺多的,這里記錄一下最終的配置結(jié)果,給需要的小伙伴參考下吧2014-11-11