Friday, September 5, 2025
HomeLanguagesMatplotlib.axis.Axis.grid() function in Python

Matplotlib.axis.Axis.grid() function in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack.

matplotlib.axis.Axis.grid() Function

The Axis.grid() function in axis module of matplotlib library is used to Configure the grid lines.
 

Syntax: Axis.grid(self, b=None, which=’major’, **kwargs) 

Parameters: This method accepts 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.

Return value: This method does not return any value. 

Below examples illustrate the matplotlib.axis.Axis.grid() function in matplotlib.axis:
Example 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()


Output: 
 

Example 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 +
      
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()


Output: 
 

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS