How to add a grid on a figure in matplotlib ?

Published: May 26, 2019

DMCA.com Protection Status

Examples of how to add a grid on a figure in matplotlib

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:

How to add a grid on a figure in matplotlib ?
How to add a grid on a figure in matplotlib ?

import matplotlib.pyplot as plt
import numpy as np

x = 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 plt
import numpy as np


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

x_min = 0
x_max = 10.0

y_min = -1.5
y_max = 1.5

x = 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()

How to add a grid on a figure in matplotlib ?
How to add a grid on a figure in matplotlib ?

Another example with a grid adapted to the sinus function:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

x_min = 0
x_max = 10.0

y_min = -1.5
y_max = 1.5

x = 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()

How to add a grid on a figure in matplotlib ?
How to add a grid on a figure in matplotlib ?

Create a logarithmic grid

Lets take for example the exponential function:

import matplotlib.pyplot as plt
import numpy as np

x_min = 0
x_max = 10.0

x = 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()

How to add a grid on a figure in matplotlib ?
How to add a grid on a figure in matplotlib ?

To have the y axis in logarithm scale, a solution is to use plt.yscale('log'):

import matplotlib.pyplot as plt
import numpy as np

x_min = 0
x_max = 10.0

x = 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')

How to add a grid on a figure in matplotlib ?
How to add a grid on a figure in matplotlib ?

References

Image

of