Skip to content

Anatomy of a Plot

Matplotlib anatomy

A typical plot has:

  • Title
  • X label / Y label
  • Axes limits
  • Ticks
  • Legend (if multiple series)
  • Grid (optional)

You can’t make a plot straight onto a blank FigureFigure — a FigureFigure is just the canvas. You always need at least one AxesAxes (a plotting area) inside it before you can draw anything. Most of the customization methods you’ll use (set_titleset_title, set_xlimset_xlim, set_xticksset_xticks, …) live on that AxesAxes object.

diagram The plot pipeline mermaid
A Figure holds Axes; each Axes owns its own ticks, limits, labels, and plotted data.

The Figure + Axes model

Figure + Axes
import matplotlib.pyplot as plt
 
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot([1, 2, 3, 4], [10, 6, 8, 4])
 
ax.set_title("Anatomy")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.grid(True, alpha=0.3)
 
plt.show()
Figure + Axes
import matplotlib.pyplot as plt
 
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot([1, 2, 3, 4], [10, 6, 8, 4])
 
ax.set_title("Anatomy")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.grid(True, alpha=0.3)
 
plt.show()

Limits and ticks

Limits and ticks
import matplotlib.pyplot as plt
 
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot([1, 2, 3, 4], [10, 6, 8, 4])
 
ax.set_xlim(0, 5)
ax.set_ylim(0, 12)
 
ax.set_xticks([1, 2, 3, 4])
ax.set_yticks([0, 4, 8, 12])
 
ax.set_title("Limits and ticks")
plt.tight_layout()
plt.show()
Limits and ticks
import matplotlib.pyplot as plt
 
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot([1, 2, 3, 4], [10, 6, 8, 4])
 
ax.set_xlim(0, 5)
ax.set_ylim(0, 12)
 
ax.set_xticks([1, 2, 3, 4])
ax.set_yticks([0, 4, 8, 12])
 
ax.set_title("Limits and ticks")
plt.tight_layout()
plt.show()

Visualize it

Every Matplotlib chart is built from the same nested parts: a Figure holds one or more Axes, and each Axes carries the title, the x/y labels, the ticks, and the plotted data. Here they are, labelled:

sketch The parts of a Matplotlib figure p5.js
A Figure holds one or more Axes; each Axes has a title, x/y labels, ticks, and the plotted data.

Tip

Use the object-oriented approach for consistent, reusable plotting.

Next

Continue to Line Plot to put the Figure + Axes model to work on your first real chart.

🧪 Try It Yourself

Exercise 1 – Set and read the title

Exercise 2 – Custom x-axis limits

Exercise 3 – Custom tick labels

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did