5.4.15Scientific Computing (Python)

Matplotlib — figure - axes architecture

1,818 words8 min readdifficulty · medium

WHAT are the pieces?

Figure — Matplotlib — figure - axes architecture

HOW do you create them? (two interfaces)

Deriving the equivalence "from scratch"

Why is plt.plot(x, y) exactly the same as ax.plot(x, y) for a single plot? Because plt.plot is literally:

gca().plot(x, y)      # gca = "get current Axes"; creates one if none exists

So pyplot is a thin wrapper that calls the same Axes method on whatever Axes is "current". The OO style just removes the guessing about which Axes is current.


Worked examples


Common mistakes (steel-manned)



Recall Feynman: explain to a 12-year-old

Imagine a scrapbook page (that's the Figure). On the page you tape down a few photo frames (those are the Axes — each frame is one little picture with its own up/down and left/right rulers). Inside each frame you draw stuff: lines, dots, a caption (those are Artists). Matplotlib sometimes remembers "the frame I touched last" so if you keep drawing, it all piles into that same frame. To stay safe, point at the exact frame you want — "frame number 3, draw here!" — instead of hoping it guessed right.


Active recall

What is the top-level Matplotlib container that becomes one saved image?
The Figure.
What does "Axes" mean in Matplotlib?
A single plot/subplot with its own x and y coordinate system (NOT the two axis lines).
What is an "Axis" (singular) in Matplotlib?
The manager of one direction's scale, ticks and tick-labels (e.g. ax.xaxis).
What is an Artist?
The base class of everything drawable: Line2D, Text, Patch, Spine, Tick, Legend, etc.
Outermost-to-innermost container hierarchy?
Figure ⊃ Axes ⊃ Artist (with Axis as the per-direction manager inside Axes).
What does plt.subplots(2,3) return?
A Figure and a 2×3 NumPy array of Axes objects.
Why is the OO interface preferred for multi-panel figures?
It addresses each Axes explicitly (ax[r,c]) so there is no ambiguous "current" Axes.
plt.plot(x,y) is shorthand for what OO call?
gca().plot(x,y) — plotting on the current Axes (created if none exists).
Which object do you call savefig on?
The Figure: fig.savefig("out.png").
Which method sets the x-label in OO style vs pyplot?
OO: ax.set_xlabel(...); pyplot: plt.xlabel(...).
What does fig.suptitle do?
Adds one title for the whole Figure, spanning all its Axes.
Why might a figure appear blank even with no error?
You plotted onto a hidden current Axes, not your ax; or forgot plt.show()/savefig.

Connections

  • 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)

Concept Map

contains list of

manages

holds

base class of

is an

calls

returns current

creates both

returns grid of

caused by wrong

Figure fig - whole canvas

Axes ax - one plot region

Axis - xaxis and yaxis

Artist - drawable base class

Line2D, Text, Spine, Tick

Pyplot interface plt.plot

OO interface fig, ax = plt.subplots

gca - get current Axes

Container confusion bugs

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Matplotlib ko samajhne ka sabse easy tarika hai use ek nested dabba (container) system ki tarah dekhna. Sabse bahar wala dabba hai Figure — yeh poora canvas hai, jaise ek kaagaz ka page, aur jab tum savefig karte ho to yahi ek image banta hai. Figure ke andar ek ya zyada Axes hote hain — yaad rakho, "Axes" ka matlab hai ek single plot (na ki do lines!). Aur har Axes ke andar jo bhi dikhta hai — line, dot, text, ticks — woh sab Artist hai. Toh formula yaad rakho: Figure ⊃ Axes ⊃ Artists.

Ab confusion kahan hota hai? Do API styles hain. Pyplot style (plt.plot, plt.xlabel) ek "current Axes" ko chupke-chupke yaad rakhta hai aur sab kuch usi par daal deta hai. Iska problem yeh hai ki jab tumhare paas 4 subplots ho, to "current" kaun hai — yeh confuse karta hai, aur labels galat plot par chali jaati hain. Isliye OO style behtar hai: fig, ax = plt.subplots() likho, aur phir ax.plot(...), ax.set_xlabel(...) karke seedha bolo "is wale dabbe mein draw karo".

Ek important spelling trap: Axes (jo plural lagta hai) actually ek single plot hai, aur Axis (jo singular lagta hai) ek direction ka ruler (x ya y) hai. Mnemonic: AxeS = a Subplot, AxiS = a Single ruler. Agar tumhara plot blank aa raha hai ya labels galat jagah ja rahe hain, 99% chance hai ki tumne galat dabbe ko address kiya — bas ax. use karke exact box batao, sab theek ho jayega. Yeh chhoti si mental model puri Matplotlib ki 80% confusion khatam kar deti hai.

Go deeper — visual, from zero

Test yourself — Scientific Computing (Python)