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
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
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:
Tip
Use the object-oriented approach for consistent, reusable plotting.
Check your understanding
What is the relationship between a Figure and an Axes?
You always need at least one Axes inside a Figure before you can draw anything, and customization methods like set_title live on the Axes.
Show answer
A โ A Figure is the canvas and holds one or more Axes, the plotting areas โ You always need at least one Axes inside a Figure before you can draw anything, and customization methods like set_title live on the Axes.
What does fig, ax = plt.subplots(figsize=(7, 4)) return?
subplots creates the figure and one Axes. figsize=(7, 4) sets its size in inches.
Show answer
A โ A Figure and an Axes โ subplots creates the figure and one Axes. figsize=(7, 4) sets its size in inches.
Which calls set the x range to 0 to 5 and the y range to 0 to 12?
set_xlim and set_ylim control the limits. set_xticks and set_yticks choose where tick marks are drawn.
Show answer
A โ ax.set_xlim(0, 5) and ax.set_ylim(0, 12) โ set_xlim and set_ylim control the limits. set_xticks and set_yticks choose where tick marks are drawn.
What does ax.grid(True, alpha=0.3) do?
alpha controls transparency, so 0.3 gives a light grid.
Show answer
A โ Draws a faint grid โ alpha controls transparency, so 0.3 gives a light grid.
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
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 โ Custom x-axis limits
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 โ Custom tick labels
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.