5.4.15 · D3Scientific Computing (Python)

Worked examples — Matplotlib — figure - axes architecture

2,254 words10 min readBack to topic

You already met the containers in the parent note: Figure (the whole page), Axes (one plot with its own rulers), Artist (anything drawn). This page does one thing: it walks through every kind of situation you can hit when you create and address these containers — and it makes you predict the answer before revealing it.

Before we start, one word we will lean on constantly:


The scenario matrix

Matplotlib's container model has a small, finite set of situations. If you can handle every cell below, nothing can surprise you. Each example that follows is tagged with the cell it covers.

# Case class The specific situation Covered by
A Single plot, OO nrows*ncols == 1 → one bare Axes Ex 1
B Return-shape: 1-D one row or one column → 1-D array Ex 2
C Return-shape: 2-D grid → 2-D array axes[r][c] Ex 3
D Degenerate / zero empty figure, no Axes, or blank plot Ex 4
E Sign/order trap pyplot's hidden current points at the wrong box Ex 5
F Figure-level vs Axes-level which action belongs to fig, which to ax Ex 6
G Real-world word problem temperature dashboard, save one file Ex 7
H Exam-style twist mixed API, indexing a flattened grid Ex 8

The "sign/quadrant" idea here is not about numbers — it is about which box is current. That hidden state is the exact analogue of a sign trap: it silently flips and ruins your output with no error. We hunt it in cells D and E.

Figure — Matplotlib — figure - axes architecture

Example 1 — Single plot (cell A)


Example 2 — One row of plots → 1-D array (cell B)

Figure — Matplotlib — figure - axes architecture

Example 3 — A grid → 2-D array (cell C)

Figure — Matplotlib — figure - axes architecture

Example 4 — Degenerate / blank (cell D)


Example 5 — The current-box "sign flip" (cell E)

Figure — Matplotlib — figure - axes architecture

Example 6 — Figure-level vs Axes-level actions (cell F)


Example 7 — Real-world word problem (cell G)


Example 8 — Exam-style twist: flatten the grid (cell H)

Figure — Matplotlib — figure - axes architecture

Recap of the matrix

Recall Did every cell get covered?

A → Ex 1 · B → Ex 2, 7 · C → Ex 3 · D → Ex 4 · E → Ex 5 · F → Ex 6 · G → Ex 7 · H → Ex 8. Every return-shape (single / 1-D / 2-D), the degenerate/blank cases, the hidden-current-box trap, the fig-vs-ax split, a real dashboard, and the flatten twist — all shown.


Connections

  • 5.4.15 Matplotlib — figure - axes architecture (Hinglish)
  • Matplotlib — pyplot vs object-oriented API
  • Matplotlib — subplots and GridSpec layout
  • Matplotlib — Artists, Line2D and styling
  • NumPy — arrays as plot data
  • Saving figures — dpi, formats, bbox
  • Scientific Computing (Python)

1 and 1

1 and N

M and N

plt.subplots nrows ncols

1x1 single Axes

one row or col 1-D array

grid 2-D array r c

axes.flat one loop