Anatomy of a Plot
Matplotlib anatomy
Section titled “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 Figure — a Figure is just the canvas.
You always need at least one Axes (a plotting area) inside it before you can draw
anything. Most of the customization methods you’ll use (set_title, set_xlim,
set_xticks, …) live on that Axes object.
flowchart TD A["fig = plt.figure()"] --> B["ax = fig.add_subplot(...)"] B --> C["ax.plot(...) / ax.bar(...) / ..."] C --> D["ax.set_title() / set_xlabel() / set_ylabel()"] D --> E["ax.set_xlim() / set_xticks()"] E --> F["plt.show() or fig.savefig()"]
The Figure + Axes model
Section titled “The Figure + Axes model”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
Section titled “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
Section titled “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:
Use the object-oriented approach for consistent, reusable plotting.
Continue to Line Plot to put the Figure + Axes model to work on your first real chart.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Set and read the title
Section titled “Exercise 1 – Set and read the title”Exercise 2 – Custom x-axis limits
Section titled “Exercise 2 – Custom x-axis limits”Exercise 3 – Custom tick labels
Section titled “Exercise 3 – Custom tick labels”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading