matplotlib grid()設(shè)置網(wǎng)格線外觀的實現(xiàn)
grid()函數(shù)概述
grid()
函數(shù)用于設(shè)置繪圖區(qū)網(wǎng)格線。
grid()
的函數(shù)簽名為matplotlib.pyplot.grid(b=None, which='major', axis='both', **kwargs)
。
grid()
的參數(shù)如下:
b
:是否顯示網(wǎng)格線。布爾值或None
,可選參數(shù)。如果沒有關(guān)鍵字參數(shù),則b
為True
,如果b
為None
且沒有關(guān)鍵字參數(shù),相當(dāng)于切換網(wǎng)格線的可見性。which
:網(wǎng)格線顯示的尺度。字符串,可選參數(shù),取值范圍為{'major', 'minor', 'both'}
,默認為'both'
。'major'
為主刻度、'minor'
為次刻度。axis
:選擇網(wǎng)格線顯示的軸。字符串,可選參數(shù),取值范圍為{'both', 'x', 'y'},默認為
'both'`。**kwargs
:Line2D
線條對象屬性。
grid()
的返回值為None
。
grid()函數(shù)演示
import matplotlib.pyplot as plt plt.subplot(341) # grid()默認樣式 plt.plot([1, 1]) plt.grid() plt.annotate('grid()', (0, 1)) plt.subplot(342) # 因為默認沒有網(wǎng)格線,所以grid(None)顯示網(wǎng)格線 plt.plot([1, 1]) plt.grid(None) plt.annotate('grid(None)', (0, 1)) plt.subplot(343) # 因為設(shè)置了網(wǎng)格線,所以grid(None)切換為不顯示網(wǎng)格線 plt.plot([1, 1]) plt.grid(True) plt.grid(None) plt.annotate('grid(None)', (0, 1)) plt.subplot(344) # 因為默認沒有網(wǎng)格線 plt.plot([1, 1]) plt.annotate("default", (0, 1)) plt.subplot(345) # 只顯示主刻度網(wǎng)格線 plt.plot([1, 1]) plt.grid(which='major') plt.annotate("which='major'", (0, 1)) plt.subplot(346) # 只顯示次刻度網(wǎng)格線,因為沒有次刻度,所以無網(wǎng)格線 plt.plot([1, 1]) plt.grid(which='minor') plt.annotate("which='minor'", (0, 1)) plt.subplot(347) # 同時顯示主刻度、次刻度網(wǎng)格線 plt.plot([1, 1]) plt.grid(which='both') plt.annotate("which='both'", (0, 1)) plt.subplot(348) plt.plot([1, 1]) # 默認同時顯示主刻度、次刻度網(wǎng)格線 plt.grid() plt.annotate("default", (0, 1)) plt.subplot(349) # 只顯示x軸網(wǎng)格線 plt.plot([1, 1]) plt.grid(axis='x') plt.annotate("axis='x'", (0, 1)) plt.subplot(3,4,10) # 只顯示y軸網(wǎng)格線 plt.plot([1, 1]) plt.grid(axis='y') plt.annotate("axis='y'", (0, 1)) plt.subplot(3,4,11) # 同時顯示xy軸網(wǎng)格線 plt.plot([1, 1]) plt.grid(axis='both') plt.annotate("axis='both'", (0, 1)) plt.subplot(3,4,12) # 默認顯示xy軸網(wǎng)格線 plt.plot([1, 1]) plt.grid() plt.annotate("default", (0, 1)) plt.show()
原理
pyplot.grid()
其實調(diào)用的是gca().grid()
,即Aexs.grid()
。
底層相關(guān)函數(shù)有:
Axis.grid()
Axes.grid()
源碼(matplotlib/Axes/_base.py
)
def grid(self, b=None, which='major', axis='both', **kwargs): cbook._check_in_list(['x', 'y', 'both'], axis=axis) if axis in ['x', 'both']: self.xaxis.grid(b, which=which, **kwargs) if axis in ['y', 'both']: self.yaxis.grid(b, which=which, **kwargs)
xaxis
為XAxis
類的實例,yaxis
為YAxis
類的實例,XAxis
和YAxis
類的基類為Axis
。
Axis.grid()
源碼(matplotlib/axis.py
)
def grid(self, b=None, which='major', **kwargs): if b is not None: if 'visible' in kwargs and bool(b) != bool(kwargs['visible']): raise ValueError( "'b' and 'visible' specify inconsistent grid visibilities") if kwargs and not b: # something false-like but not None cbook._warn_external('First parameter to grid() is false, ' 'but line properties are supplied. The ' 'grid will be enabled.') b = True which = which.lower() cbook._check_in_list(['major', 'minor', 'both'], which=which) gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()} if 'grid_visible' in gridkw: forced_visibility = True gridkw['gridOn'] = gridkw.pop('grid_visible') else: forced_visibility = False if which in ['minor', 'both']: if b is None and not forced_visibility: gridkw['gridOn'] = not self._minor_tick_kw['gridOn'] elif b is not None: gridkw['gridOn'] = b self.set_tick_params(which='minor', **gridkw) if which in ['major', 'both']: if b is None and not forced_visibility: gridkw['gridOn'] = not self._major_tick_kw['gridOn'] elif b is not None: gridkw['gridOn'] = b self.set_tick_params(which='major', **gridkw) self.stale = True
到此這篇關(guān)于matplotlib grid()設(shè)置網(wǎng)格線外觀的實現(xiàn)的文章就介紹到這了,更多相關(guān)matplotlib grid()網(wǎng)格線內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python程序中的觀察者模式結(jié)構(gòu)編寫示例
觀察者模式是最常用的設(shè)計模式之一,旨在觀察目標(biāo)和觀察者之間建立一個抽象的耦合,減少對象之間的耦合,這里我們就來看一下Python程序中的觀察者模式結(jié)構(gòu)編寫示例2016-05-05pytorch中交叉熵損失(nn.CrossEntropyLoss())的計算過程詳解
今天小編就為大家分享一篇pytorch中交叉熵損失(nn.CrossEntropyLoss())的計算過程詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01學(xué)會使用Python?Configparser處理ini文件模塊
這篇文章主要為大家介紹了使用Python?Configparser處理ini文件模塊的學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06pyspark對Mysql數(shù)據(jù)庫進行讀寫的實現(xiàn)
這篇文章主要介紹了pyspark對Mysql數(shù)據(jù)庫進行讀寫的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12