I found matplotlib unintuitive and frustrating to learn. Here's what I wish I'd learned at the start. Most quotes and code came straight from the documentation.

Figures and axes

Matplotlib graphs your data on

The simplest way of creating a figure with an axes is using pyplot.subplots. We can then use Axes.plot to draw some data on the axes

fig, ax = plt.subplots()  # Create a figure containing a single axes.
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])  # Plot some data on the axes.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b0ecf976-f9a4-4bff-ac51-8d473cfe5a11/Untitled.png

However, we didn't have to create the figure and axes. We could have just used this line of code for the same result:

plt.plot([1, 2, 3, 4], [1, 4, 2, 3])

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/03ee13ee-7ea8-4b82-aca3-cd168012de54/Untitled.png

The first method is 'object oriented style' the second is 'pyplot style'. Both are valid, but it helps to be consistent which we are using.

Anatomy of a figure

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ae463350-cae6-4424-a4bd-4dc88fa30b91/Untitled.png

Figure

The whole figure. The figure keeps track of all the child Axes, a smattering of 'special' artists (titles, figure legends, etc), and the canvas. (Don't worry too much about the canvas, it is crucial as it is the object that actually does the drawing to get you your plot, but as the user it is more-or-less invisible to you). A figure can contain any number of Axes, but will typically have at least one.

fig = plt.figure()
# an empty figure with no Axes

<Figure size 432x288 with 0 Axes>

fig, ax = plt.subplots()
# a figure with a single Axes

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/38d7cbfa-9a3a-4736-afee-dc90af67af36/Untitled.png

fig, axs = plt.subplots(2, 2)
#figure w/ a 2x2 grid of Axes

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b5e82971-3c37-407f-a065-4894ee53fae4/Untitled.png

It's convenient to create the axes together with the figure, but you can also add axes later on, allowing for more complex axes layouts.

Axes

What I would call one 'plot', matplotlib calls an 'axes'.