Last Updated : 12 Jul, 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.
Syntaxmatplotlib.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.
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4