Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate25 min
Anatomy of a Figure hero Module 25 Lesson 2Anatomy of a Figure hero Module 25 Lesson 2

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.

A chart with its figure, axes, title, labels, and legend labelledA chart with its figure, axes, title, labels, and legend labelled

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.

python
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:

text
Figure
Axes
axes on this figure: 1

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

python
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:

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

python
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()
A titled line chart with named axesA titled line chart with named axes

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.

python
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:

text
Signups last week
True

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