Draw a grid in a Matplotlib plot

Python

Matplotlib

Axes

Add grid

Luc B.

Python

Matplotlib

Axis grids allow users to easily determine the values associated with particular data points. While they can make plots feel cluttered, they are essential for certain visualizations.

Code Example

Use the plt.grid() function to add an axis grid.

import matplotlib.pyplot as plt

# Add a grid here
plt.grid()

plt.show()

png

More Examples

Object Oriented Interface

Matplotlib's object oriented interface supports the same functionality with the Axes.grid() function.

ax = plt.axes()

# Add a grid here
ax.grid()

plt.show()

png