📌  相关文章
📜  Python中的 Matplotlib.axis.Axis.grid()函数

📅  最后修改于: 2022-05-13 01:55:32.976000             🧑  作者: Mango

Python中的 Matplotlib.axis.Axis.grid()函数

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。它是Python中用于二维数组图的惊人可视化库,用于处理更广泛的 SciPy 堆栈。

matplotlib.axis.Axis.grid()函数

matplotlib 库的轴模块中的Axis.grid()函数用于配置网格线。

下面的示例说明了 matplotlib.axis 中的 matplotlib.axis.Axis.grid()函数:
示例 1:

Python3
# Implementation of matplotlib function 
from matplotlib.axis import Axis  
import matplotlib.pyplot as plt 
import numpy as np 
       
fig, ax = plt.subplots() 
ax.plot([1, 2, 3]) 
ax.xaxis.grid() 
    
fig.suptitle("Matplotlib.axis.Axis.grid()\
Function Example", fontsize = 12, fontweight ='bold')
  
plt.show()


Python3
# Implementation of matplotlib function 
from matplotlib.axis import Axis  
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('\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') 
    
fig.suptitle("Matplotlib.axis.Axis.grid()\
Function Example", fontsize = 12, fontweight ='bold') 
  
plt.show()


输出:

示例 2:

Python3

# Implementation of matplotlib function 
from matplotlib.axis import Axis  
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('\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') 
    
fig.suptitle("Matplotlib.axis.Axis.grid()\
Function Example", fontsize = 12, fontweight ='bold') 
  
plt.show()

输出: