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)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 如何基于Python Matplotlib實現(xiàn)網(wǎng)格動畫