grid()函数用于设置绘图区网格线。
grid()的函数签名为matplotlib.pyplot.grid(b=None, which='major', axis='both', **kwargs)。
grid()的参数如下:
grid()的返回值为None。
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)
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
到此这篇关于matplotlib grid()设置网格线外观的实现的文章就介绍到这了,更多相关matplotlib grid()网格线内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。