Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展。轴类包含大多数图形元素:Axis,Tick,Line2D,Text,Polygon等,并设置坐标系。 Axes实例通过callbacks属性支持回调。
matplotlib.axes.Axes.grid()函数
matplotlib库的axiss模块中的Axes.grid()函数用于配置网格线。
用法: Axes.grid(self, b=None, which=’major’, axis=’both’, **kwargs)
参数:此方法接受以下参数。
- b:此参数是可选参数,是否显示网格线。
- which:此参数也是可选参数,是应用更改的网格线。
- axis:此参数也是可选参数,它是应用更改的轴。
返回值:此方法不返回任何值。
以下示例说明了matplotlib.axes中的matplotlib.axes.Axes.grid()函数:
范例1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax.grid()
ax.set_title('matplotlib.axes.Axes.grid() Example\n',
fontsize = 12, fontweight ='bold')
plt.show()
输出:
范例2:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-5, 5, 0.01)
y1 = -3 * x*x + 10 * x + 10
y2 = 3 * x*x + x
fig, [ax, ax1] = plt.subplots(2, 1,
sharex = True)
ax.plot(x, y1, x, y2, color ='black')
ax.fill_between(x, y1, y2, where = y2 >y1,
facecolor ='green',
alpha = 0.8)
ax.fill_between(x, y1, y2, where = y2 <= y1,
facecolor ='black',
alpha = 0.8)
ax.xaxis.grid(True, color ="black")
ax.set_title('matplotlib.axes.Axes.grid() \
Example\n\n Grid in X-axis',
fontsize = 12, fontweight ='bold')
ax1.plot(x, y1, x, y2, color ='black')
ax1.fill_between(x, y1, y2, where = y2 >y1,
facecolor ='green',
alpha = 0.8)
ax1.fill_between(x, y1, y2, where = y2 <= y1,
facecolor ='black',
alpha = 0.8)
ax1.yaxis.grid(True, color ="black")
ax1.set_title('Grid in y-axis',
fontsize = 12, fontweight ='bold')
plt.show()
输出:
注:本文由纯净天空筛选整理自SHUBHAMSINGH10大神的英文原创作品 Matplotlib.axes.Axes.grid() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。