Grid, axes, and labels
Now we can learn about some features that we can add to plots, and some features to control them better.
Adding a grid
In the previous images, we saw that the background of the figure was completely blank. While it might be nice for some plots, there are situations where having a reference system would improve the comprehension of the plot—for example with multiline plots. We can add a grid to the plot by calling the grid()
function; it takes one parameter, a Boolean value, to enable (if True
) or disable (if False
) the grid:
In [1]: import matplotlib.pyplot as plt In [2]: import numpy as np In [3]: x = np.arange(1, 5) In [4]: plt.plot(x, x*1.5, x, x*3.0, x, x/3.0) Out[4]: [<matplotlib.lines.Line2D object at 0x8fcc20c>, <matplotlib.lines.Line2D object at 0x8fcc50c>, <matplotlib.lines.Line2D object at 0x8fcc84c>] In [5]: plt.grid(True) In [6]: plt.show()
We can see the grid in the following screenshot, as a result of executing the preceding code:
