Wednesday, July 8, 2026
HomeLanguagesMatplotlib.pyplot.grid() in Python

Matplotlib.pyplot.grid() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface.

matplotlib.pyplot.grid() Function

The grid() function in pyplot module of matplotlib library is used to configure the grid lines.

Syntax: matplotlib.pyplot.grid(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.

Below examples illustrate the matplotlib.pyplot.grid() function in matplotlib.pyplot:

Example #1:




# Implementation of matplotlib function   
import matplotlib.pyplot as plt
import numpy as np
      
plt.plot([1, 2, 3])
plt.grid()
  
plt.title('matplotlib.pyplot.grid() function \
Example\n\n', fontweight ="bold")
plt.show()


Output:

Example #2:




# Implementation of matplotlib function   
import numpy as np
import matplotlib.pyplot as plt
  
np.random.seed(19680801)
  
val, res = 100, 15
x = np.sin(val + res * np.random.randn(10000)) - np.cos(val + res * np.random.randn(10000))
  
n, bins, patches = plt.hist(x, 200
                            density = True
                            facecolor ='g'
                            alpha = 0.5)
  
plt.grid(True)
  
plt.title('matplotlib.pyplot.grid() function \
Example\n\n', fontweight ="bold")
  
plt.show()


Output:

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6901 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12111 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6978 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS