Matplotlib.axes.axes.grid()
Matplotlib是Python中的一个库,它是NumPy库的数值-数学扩展。Axes包含了大多数图形元素:Axis、Tick、Line2D、Text、Polygon等,并设置坐标系。Axes的实例通过callbacks属性支持回调。
函数:matplotlib.axes.axes.grid()
matplotlib库的Axes模块中的Axes.grid()函数用于配置网格线。
语法:
Axes.grid(self, b=None, which=’major’, axis=’both’, **kwargs)
参数:此方法接受以下参数。
- b:可选参数,是否显示网格线。
- 该参数也是一个可选参数,它是应用更改的网格线。
- axis:该参数也是一个可选参数,它是应用更改的轴。
Returns:该方法不返回任何值。
下面的例子演示了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()
输出: