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.
Matplotlib graphs your data on
- Figures (i.e., windows, Jupyter widgets, etc.), each of which can contain one or more
- Axes (i.e., an area where points can be specified in terms of cartesian or polar coordinates).
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.
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])
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.
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
fig, axs = plt.subplots(2, 2)
#figure w/ a 2x2 grid of Axes
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.
What I would call one 'plot', matplotlib calls an 'axes'.