O'Reilly logo

Matplotlib for Python Developers by Sandro Tosi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required