When preparing graphics, we might need to have a quick guess of the coordinates of any part of a figure. Adding a grid to the figure is a natural way to improve the readability of a figure. In this recipe, we are going to see how to add a grid to a figure.
matplotlib's grid functionality is controlled with the pyplot.grid()
function.
import numpy as np import matplotlib.pyplot as plt X = np.linspace(-4, 4, 1024) Y = .25 * (X + 4.) * (X + 1.) * (X - 2.) plt.plot(X, Y, c = 'k') plt.grid(True, lw = 2, ls = '--', c = '.75') plt.show()
This script will show a curve with a grid in the background. The grid is aligned to the ticks of the axes' legend as shown in the following graph: