Lesson 02
Anatomy of a Figure
Learn the parts of a Matplotlib chart: the figure that holds everything, the axes you draw on, and the title, labels, ticks, and legend.
Every chart you draw is made of the same handful of parts, and knowing their names turns error messages and documentation from a puzzle into instructions. The figure is the whole picture. The axes are the plotting area inside it, with its two axis lines. Everything you can see on top of that, the line, the ticks, the title, the legend, is an artist.
Figure and axes
The figure is the canvas: the page, the window, the saved file. The axes are one plotting area drawn on that canvas, and a figure can hold several. Confusingly, one set of axes is a single plot, while an axis is one of its two number lines.
import matplotlib.pyplot as plt
figure = plt.figure()
area = figure.add_subplot(1, 1, 1)
print(type(figure).__name__)
print(type(area).__name__)
print("axes on this figure:", len(figure.axes))Output:
Figure
Axes
axes on this figure: 1Choosing the size
figsize sets the width and height of the figure in inches, and dpi sets how many pixels make up each inch. Multiply them and you have the pixel size of the image you will save. A wide figure gives a long time axis room to breathe.
import matplotlib.pyplot as plt
figure = plt.figure(figsize=(10, 4), dpi=100)
print("inches:", figure.get_size_inches())
print("pixels:", figure.get_size_inches() * figure.dpi)
plt.close(figure)Output:
inches: [10. 4.]
pixels: [1000. 400.]The labels a reader needs
A chart with no words is a decoration. Four calls turn it into a statement: a title that says what the chart is about, a name for each axis so the numbers mean something, and a legend when more than one thing is drawn.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]
plt.plot(days, signups, label="signups")
plt.title("Signups last week")
plt.xlabel("day")
plt.ylabel("people")
plt.legend()
plt.show()Two ways to say the same thing
plt.title(...) works on whichever axes is current, which is short and fine for one chart. Holding the axes in a variable and calling area.set_title(...) says exactly which plot you mean, which is what you want once a figure has several. Both are in wide use, so you need to read both.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]
figure, area = plt.subplots(figsize=(8, 4))
area.plot(days, signups)
area.set_title("Signups last week")
area.set_xlabel("day")
area.set_ylabel("people")
print(area.get_title())
print(figure.axes[0] is area)
plt.show()Output:
Signups last week
Trueplt.subplots() is the usual opening line of a serious chart: it builds the figure and the axes together and hands you both.
Key takeaways
-
The figure is the whole canvas; the axes are one plotting area on it
-
An axis is one number line; axes is a whole plot
-
figsize is in inches, and figsize times dpi gives the pixel size
-
plt.title() acts on the current axes; area.set_title() names the plot
-
Next: Your First Line Plot
-
Previous: What Is Matplotlib
Loading playground…
Check your understanding
Anatomy of a Figure Quiz
What is the figure in Matplotlib?
1 / 10