首页 > 数据分析

matplotlib grid()设置网格线外观的实现

时间:2021-03-27 数据分析 查看: 805

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()网格线内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!

展开全文
上一篇:matplotlib之pyplot模块坐标轴标签设置使用(xlabel()、ylabel())
下一篇:python matplotlib工具栏源码探析二之添加、删除内置工具项的案例
输入字:
相关知识
python数据挖掘使用Evidently创建机器学习模型仪表板

在本文中,我们将探索 Evidently 并创建交互式报告/仪表板。有需要的朋友欢迎大家收藏学习,希望能够有所帮助,祝大家多多进步早日升职加薪

Python多进程共享numpy 数组的方法

本文章主要介绍了Python多进程共享numpy 数组的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

python数据分析近年比特币价格涨幅趋势分布

这篇文章主要为大家介绍了python分析近年来比特币价格涨幅趋势的数据分布,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步

python调用matlab的方法详解

这篇文章主要为大家介绍了python调用matlab,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助