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


Posted in Python onFebruary 22, 2021

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()函数演示

matplotlib 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 相关文章推荐
编写自定义的Django模板加载器的简单示例
Jul 21 Python
5款非常棒的Python工具
Jan 05 Python
python爬虫获取淘宝天猫商品详细参数
Jun 23 Python
TensorFlow平台下Python实现神经网络
Mar 10 Python
numpy.transpose对三维数组的转置方法
Apr 17 Python
python线程池threadpool实现篇
Apr 27 Python
Pycharm设置utf-8自动显示方法
Jan 17 Python
OpenCV模板匹配matchTemplate的实现
Oct 18 Python
python 实现批量替换文本中的某部分内容
Dec 13 Python
jupyter notebook运行命令显示[*](解决办法)
May 18 Python
Python的控制结构之For、While、If循环问题
Jun 30 Python
python绘制雷达图实例讲解
Jan 03 Python
浅析python连接数据库的重要事项
Feb 22 #Python
python实现学生信息管理系统源码
Feb 22 #Python
python实现简单的学生管理系统
Feb 22 #Python
matplotlib之pyplot模块坐标轴标签设置使用(xlabel()、ylabel())
Feb 22 #Python
matplotlib之pyplot模块之标题(title()和suptitle())
Feb 22 #Python
matplotlib源码解析标题实现(窗口标题,标题,子图标题不同之间的差异)
Feb 22 #Python
python利用后缀表达式实现计算器功能
Feb 22 #Python
You might like
smarty section简介与用法分析
2008/10/03 PHP
百度地图经纬度转换到腾讯地图/Google 对应的经纬度
2015/08/28 PHP
Thinkphp框架 表单自动验证登录注册 ajax自动验证登录注册
2016/12/27 PHP
用于table内容排序
2006/07/21 Javascript
JavaScript学习点滴 call、apply的区别
2010/10/22 Javascript
分别用marquee和div+js实现首尾相连循环滚动效果,仅3行代码
2011/09/21 Javascript
面向对象继承实例(a如何继承b问题)(自写)
2013/07/01 Javascript
Jquery显示和隐藏元素或设为只读(含Ligerui的控件禁用,实例说明介绍)
2013/07/09 Javascript
javascript中数组array及string的方法总结
2014/11/28 Javascript
JS动态显示表格上下frame的方法
2015/03/31 Javascript
jQuery validate+artdialog+jquery form实现弹出表单思路详解
2016/04/18 Javascript
JS扩展String.prototype.format字符串拼接的功能
2018/03/09 Javascript
原生JS实现DOM加载完成马上执行JS代码的方法
2018/09/07 Javascript
初探Vue3.0 中的一大亮点Proxy的使用
2018/12/06 Javascript
vue中利用iscroll.js解决pc端滚动问题
2020/02/15 Javascript
Nuxt页面级缓存的实现
2020/03/09 Javascript
Python模块学习 re 正则表达式
2011/05/19 Python
初步介绍Python中的pydoc模块和distutils模块
2015/04/13 Python
深入理解Python对Json的解析
2017/02/14 Python
Python网络爬虫神器PyQuery的基本使用教程
2018/02/03 Python
python中join()方法介绍
2018/10/11 Python
Python numpy.array()生成相同元素数组的示例
2018/11/12 Python
初探利用Python进行图文识别(OCR)
2019/02/26 Python
python 中的9个实用技巧,助你提高开发效率
2020/08/30 Python
利用Python实现自动扫雷小脚本
2020/12/17 Python
html5 worker 实例(一) 为什么测试不到效果
2013/06/24 HTML / CSS
HTML5如何使用SVG的方法示例
2019/01/11 HTML / CSS
香港中原电器网上商店:Chung Yuen
2019/06/26 全球购物
个人担保书格式范文
2014/05/12 职场文书
护士节策划方案
2014/05/19 职场文书
交通局领导班子群众路线教育实践活动对照检查材料思想汇报
2014/10/09 职场文书
感谢信怎么写
2015/01/21 职场文书
旷工辞退通知书
2015/04/17 职场文书
法律服务所工作总结
2015/08/10 职场文书
情感电台广播稿
2015/08/18 职场文书
浅析MySQL如何实现事务隔离
2021/06/26 MySQL