Examples of how to add a grid on a figure in matplotlib
Table of contents
Using grid() function
To add a grid in the background on a figure in matplotlib, a solution is to use the pyplot function grid() pour connaitre l'ensemble des arguments possibles). Example:

import matplotlib.pyplot as pltimport numpy as npx = np.arange(-5,5, .01)y = np.sin(2*np.pi*x)plt.xlim(-5,5)plt.ylim(-1.5,1.5)plt.plot(x,y)plt.grid()plt.show()
Note: it is also possible to add a grid for only one axis using the option plt.grid(axis='y') or plt.grid(axis='x').
Customize the grid
To customize a grid, a solution is to use the functions set_xticks and set_yticks:
import matplotlib.pyplot as pltimport numpy as npfig = plt.figure()ax = fig.add_subplot(1, 1, 1)x_min = 0x_max = 10.0y_min = -1.5y_max = 1.5x = np.arange(x_min, x_max, .01)y = np.sin(np.pi*x)plt.xlim(x_min,x_max)plt.ylim(y_min,y_max)plt.plot(x,y)grid_x_ticks = np.arange(x_min, x_max, 0.2)grid_y_ticks = np.arange(y_min, y_max, 0.2)ax.set_xticks(grid_x_ticks , minor=True)ax.set_yticks(grid_y_ticks , minor=True)ax.grid(which='both')ax.grid(which='minor', alpha=0.2, linestyle='--')plt.title('How to add a grid on a figure in matplotlib ?', fontsize=8)plt.savefig("matplotlib_grid_01.png", bbox_inches='tight')plt.close()

Another example with a grid adapted to the sinus function:
import matplotlib.pyplot as pltimport numpy as npfig = plt.figure()ax = fig.add_subplot(1, 1, 1)x_min = 0x_max = 10.0y_min = -1.5y_max = 1.5x = np.arange(x_min, x_max, .01)y = np.sin(x)plt.xlim(x_min,x_max)plt.ylim(y_min,y_max)plt.plot(x,y)grid_x_ticks_minor = np.arange(x_min, x_max, 0.2 )grid_x_ticks_major = np.arange(x_min, x_max, np.pi/2.0 )ax.set_xticks(grid_x_ticks_minor, minor=True)ax.set_xticks(grid_x_ticks_major)grid_y_ticks_minor = np.arange(y_min, y_max, 0.2 )grid_y_ticks_major = [-1.0, 0.0, 1.0]ax.set_yticks(grid_y_ticks_minor, minor=True)ax.set_yticks(grid_y_ticks_major)ax.set_yticks(grid_y_ticks , minor=True)ax.grid(which='both', linestyle='--')ax.grid(which='minor', alpha=0.2)plt.title('How to add a grid on a figure in matplotlib ?', fontsize=8)plt.savefig("matplotlib_grid_02.png", bbox_inches='tight')plt.close()

Create a logarithmic grid
Lets take for example the exponential function:
import matplotlib.pyplot as pltimport numpy as npx_min = 0x_max = 10.0x = np.arange(x_min, x_max, .01)y = np.exp(x)plt.plot(x,y)plt.xlim(x_min,x_max)plt.ylim(np.exp(x_min),np.exp(x_max))plt.grid(True,which="both", linestyle='--')plt.title('How to add a grid on a figure in matplotlib ?', fontsize=8)plt.savefig("matplotlib_grid_03.png", bbox_inches='tight')plt.close()

To have the y axis in logarithm scale, a solution is to use plt.yscale('log'):
import matplotlib.pyplot as pltimport numpy as npx_min = 0x_max = 10.0x = np.arange(x_min, x_max, .01)y = np.exp(x)plt.plot(x,y)plt.xlim(x_min,x_max)plt.ylim(np.exp(x_min),np.exp(x_max))plt.yscale('log')plt.grid(True,which="both", linestyle='--')plt.title('How to add a grid on a figure in matplotlib ?', fontsize=8)plt.savefig("matplotlib_grid_04.png", bbox_inches='tight')

References
| Links | Site |
|---|---|
| matplotlib.pyplot.grid() | Matplotlib doc |
| How do I draw a grid onto a plot in Python? | stackoverflow |
| Matplotlib how to show logarithmically spaced grid lines at all ticks on a log-log plot? | stackoverflow |
| How to add a grid line at a specific location in matplotlib plot? | stackoverflow |
| Change grid interval and specify tick labels in Matplotlib | stackoverflow |
| How to create major and minor gridlines with different linestyles in Python | stackoverflow |
