-
Book Overview & Buying
-
Table Of Contents
matplotlib Plotting Cookbook
By :
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:

Adding a grid is as simple as calling the pyplot.grid() function with True as the argument. A grid is comprised of lines and as such, pyplot.grid() accepts line style parameters, such as linewidth, linestyle, or color. These parameters will apply to the lines used to draw the...
Change the font size
Change margin width
Change background colour