matplotlib grid()設置網格線外觀的實現

grid()函數概述

grid()函數用於設置繪圖區網格線。
grid()的函數簽名為matplotlib.pyplot.grid(b=None, which='major', axis='both', **kwargs)
grid()的參數如下:

  • b:是否顯示網格線。佈爾值或None,可選參數。如果沒有關鍵字參數,則bTrue,如果bNone且沒有關鍵字參數,相當於切換網格線的可見性。
  • which:網格線顯示的尺度。字符串,可選參數,取值范圍為{'major', 'minor', 'both'},默認為'both''major'為主刻度、'minor'為次刻度。
  • axis:選擇網格線顯示的軸。字符串,可選參數,取值范圍為{'both', 'x', 'y'},默認為‘both’`。
  • **kwargsLine2D線條對象屬性。

grid()的返回值為None

grid()函數演示

在這裡插入圖片描述

import matplotlib.pyplot as plt

plt.subplot(341)
# grid()默認樣式
plt.plot([1, 1])
plt.grid()
plt.annotate('grid()', (0, 1))
plt.subplot(342)
# 因為默認沒有網格線,所以grid(None)顯示網格線
plt.plot([1, 1])
plt.grid(None)
plt.annotate('grid(None)', (0, 1))
plt.subplot(343)
# 因為設置瞭網格線,所以grid(None)切換為不顯示網格線
plt.plot([1, 1])
plt.grid(True)
plt.grid(None)
plt.annotate('grid(None)', (0, 1))
plt.subplot(344)
# 因為默認沒有網格線
plt.plot([1, 1])
plt.annotate("default", (0, 1))
plt.subplot(345)
# 隻顯示主刻度網格線
plt.plot([1, 1])
plt.grid(which='major')
plt.annotate("which='major'", (0, 1))
plt.subplot(346)
# 隻顯示次刻度網格線,因為沒有次刻度,所以無網格線
plt.plot([1, 1])
plt.grid(which='minor')
plt.annotate("which='minor'", (0, 1))
plt.subplot(347)
# 同時顯示主刻度、次刻度網格線
plt.plot([1, 1])
plt.grid(which='both')
plt.annotate("which='both'", (0, 1))
plt.subplot(348)
plt.plot([1, 1])
# 默認同時顯示主刻度、次刻度網格線
plt.grid()
plt.annotate("default", (0, 1))
plt.subplot(349)
# 隻顯示x軸網格線
plt.plot([1, 1])
plt.grid(axis='x')
plt.annotate("axis='x'", (0, 1))
plt.subplot(3,4,10)
# 隻顯示y軸網格線
plt.plot([1, 1])
plt.grid(axis='y')
plt.annotate("axis='y'", (0, 1))
plt.subplot(3,4,11)
# 同時顯示xy軸網格線
plt.plot([1, 1])
plt.grid(axis='both')
plt.annotate("axis='both'", (0, 1))
plt.subplot(3,4,12)
# 默認顯示xy軸網格線
plt.plot([1, 1])
plt.grid()
plt.annotate("default", (0, 1))
plt.show()

原理

pyplot.grid()其實調用的是gca().grid(),即Aexs.grid()

底層相關函數有:
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)

xaxisXAxis類的實例,yaxisYAxis類的實例,XAxisYAxis類的基類為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

到此這篇關於matplotlib grid()設置網格線外觀的實現的文章就介紹到這瞭,更多相關matplotlib grid()網格線內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!