Matplotlib.pyplot.grid() in Python
Last Updated :
14 Jun, 2025
matplotlib.pyplot.grid() function in Matplotlib is used to toggle the visibility of grid lines in a plot. Grid lines help improve readability of a plot by adding reference lines at major and/or minor tick positions along the axes. Example:
Python
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
plt.grid(True)
plt.show()
Output
Basic GridExplanation: This code plots a basic line graph and enables the grid for both axes, making it easier to read the plot.
Syntax
matplotlib.pyplot.grid(b=None, which='major', axis='both', **kwargs)
Parameters:
Parameter | Type | Description |
---|
b | bool or None, optional | Turns grid on (True), off (False), or toggles (None). |
---|
which | 'major', 'minor', 'both' | Specifies which grid lines to show. |
---|
axis | 'both', 'x', 'y' | Selects which axis to draw the grid on. |
---|
kwargs | Additional line properties | Customizes grid appearance (e.g., color, linewidth, linestyle). |
---|
Returns: This function modifies the grid of the current Axes in-place, meaning it doesn't return anything but updates the plot's grid properties directly.
Examples
Example 1: In this example, we plot a sine wave and customize the grid lines by setting the grid to be dashed ('--'), with a red color and reduced opacity (alpha=0.5).
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
plt.grid(True, linestyle='--', linewidth=0.7, color='red', alpha=0.5)
plt.show()
Output
Dashed Red GridExplanation: we generate a sine wave using np.sin(x) and plot it using Matplotlib. The grid is then customized to appear dashed, with a red color and semi-transparent effect (alpha=0.5), making it less intrusive on the plot.
Example 2: In this example, we plot a simple set of points and enable the grid only on the x-axis to focus on horizontal alignment.
Python
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
plt.grid(True, axis='x')
plt.show()
Output
X-Axis GridExplanation: We create a simple plot with points [1, 2, 3] on the x-axis and [1, 4, 9] on the y-axis. The grid is then enabled only on the x-axis by setting axis='x'. This ensures that only the horizontal grid lines are shown, making the x-axis positioning clearer.
Example 3: In this example, we plot a quadratic curve and enable grid lines for both the x and y axes. The grid lines are set to be solid, green and thicker for better visibility.
Python
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
ax.plot(x, y)
plt.grid(True, which='major', axis='both', linestyle='-', color='green', linewidth=1.5)
plt.show()
Output
Quadratic GridExplanation: We plot a quadratic curve with x-values [0, 1, 2, 3, 4] and their squares for y-values. The grid is customized to be solid, green and thicker (linewidth=1.5), enabled for both axes to enhance readability.
Similar Reads
Matplotlib.pyplot.gci() 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. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Matplotlib.pyplot.gca() in Python Matplotlib is a library in Python and it is a numerical - mathematical extension for the NumPy library. Pyplot is a state-based interface to a Matplotlib module that provides a MATLAB-like interface.  matplotlib.pyplot.gca() Function The gca() function in pyplot module of matplotlib library is used
2 min read
Matplotlib.pyplot.csd() in Python csd() method in Matplotlib is used to compute and plot the Cross Spectral Density (CSD) of two signals. It helps analyze the frequency domain relationship between two time series, revealing how the power of one signal is distributed relative to the other signal across frequencies. It's key features
4 min read
Matplotlib.pyplot.ion() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. The matplotlib.pyplot.ion() is used to turn on interactive mode. To check the status of
4 min read
Matplotlib.pyplot.gcf() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.pyplot.gcf() matplotlib.pyplot.gcf() is primarily used to get the current fi
2 min read
matplotlib.pyplot.figure() 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. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Matplotlib.pyplot.draw() 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.draw() Function The draw() function in pyplot module of matplotlib library is used to r
1 min read
Matplotlib.pyplot.axis() in Python axis() function in Matplotlib is used to get or set properties of the x- and y-axis in a plot. It provides control over axis limits, aspect ratio and visibility, allowing customization of the plotâs coordinate system and view. It's key feature includes:Gets or sets the axis limits [xmin, xmax, ymin,
3 min read
Matplotlib.pyplot.ginput() 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. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Matplotlib.pyplot.cla() 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. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
1 min read