Python中的 Matplotlib.axes.Axes.grid()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.grid()函数
matplotlib 库的 axes 模块中的Axes.grid()函数用于配置网格线。
Syntax: Axes.grid(self, b=None, which=’major’, axis=’both’, **kwargs)
Parameters: This method accept the following parameters.
- b : This parameter is an optional parameter, whether to show the grid lines or not.
- which : This parameter is also an optional parameter and it is the grid lines to apply the changes on.
- axis :This parameter is also an optional parameter and it is the axis to apply the changes on.
Returns:This method does not return any value.
下面的示例说明了 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()
输出: