5.4.15 · Coding › Scientific Computing (Python)
Intuition Woh ek mental model jo Matplotlib unlock kar deta hai
Matplotlib ek set of nested containers hai. Ek Figure poora canvas hota hai (kagaz ki sheet). Uske andar ek ya zyada Axes rehte hain (ek single plot apne coordinate system ke saath). Har Axes ke andar Artists rehte hain — lines, text, ticks, spines, legends — jo bhi cheez dikhai deti hai woh ek Artist hai.
YEH kyun important hai: almost har confusing Matplotlib bug ("kuch show nahi ho raha", "labels galat plot pe ja rahe hain", "kyun do windows?") actually ek container confusion hai — aapne kisi Artist ko galat box mein daala, ya galat box ko address kiya.
Definition Container hierarchy
Figure (fig): top-level container. Canvas, background, aur Axes ki ek list hold karta hai. Ek Figure = ek image file jab aap save karte hain.
Axes (ax): ek single plotting region jisme x-axis aur y-axis hote hain (note: "Axes" matlab ek plot hai, na ki "do axis lines" — confusing naming hai!). Yahan plot, scatter, set_xlabel rehte hain.
Axis (ax.xaxis, ax.yaxis): ek direction ka scale, ticks, tick locations aur labels manage karta hai.
Artist : har drawable cheez ka abstract base class (Line2D, Text, Patch, Spine, Tick...).
Intuition "Axes" kyun, "Plot" kyun nahi?
Historically ek coordinate frame apne axes se define hota tha. Axes ≈ "ek single plot" padho aur aap kabhi confused nahi honge. Actual lines/ticks Axis (singular) hain.
Definition Do APIs — andar same objects
Pyplot (implicit) interface: plt.plot(...) — Matplotlib ek hidden "current figure" aur "current Axes" maintain karta hai aur silently unme add karta rehta hai. One-off scripts ke liye fast hai.
Object-oriented (explicit) interface: aap fig, ax = plt.subplots() lete hain aur ax pe methods call karte hain. Kisi bhi reusable, multi-panel, ya functions mein preferred hai.
plt.plot(x, y) ek single plot ke liye ax.plot(x, y) ke exactly same kyun hai? Kyunki plt.plot literally yeh hai:
gca().plot(x, y) # gca = "get current Axes"; agar koi nahi hai toh ek banata hai
Toh pyplot ek thin wrapper hai jo same Axes method call karta hai jo bhi Axes "current" hai uspe. OO style bas yeh guessing hata deta hai ki kaun sa Axes current hai.
Worked example 1 — Single plot, OO style (safe default)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace( 0 , 2 * np.pi, 200 )
fig, ax = plt.subplots( figsize = ( 5 , 3 )) # 1 Figure, 1 Axes
ax.plot(x, np.sin(x), label = "sin" ) # ax mein ek Line2D Artist add karta hai
ax.set_xlabel( "x [rad]" ) # Yeh step kyun? labels Axes ke hain, Figure ke nahi
ax.set_ylabel( "amplitude" )
ax.set_title( "One Axes" )
ax.legend() # Kyun? legend IS Axes mein Artists ke labels padhta hai
fig.savefig( "out.png" , dpi = 150 ) # Kyun fig? saving ek Figure-level action hai
ax.set_xlabel kyun, plt.xlabel kyun nahi? Dono ek plot ke liye kaam karte hain, lekin ax. exactly batata hai kaunsa box hai, toh jab aap doosra panel add karte hain toh yeh kabhi nahi tootega.
Worked example 2 — Ek 2×2 grid (jahan pyplot dangerous ho jaata hai)
fig, axes = plt.subplots( 2 , 2 , figsize = ( 6 , 5 ))
axes[ 0 , 0 ].plot([ 0 , 1 ],[ 0 , 1 ]) # top-left
axes[ 1 , 1 ].scatter([ 1 , 2 ],[ 3 , 1 ]) # bottom-right
fig.suptitle( "Grid" ) # Kyun fig.? ek figure-wide title jo saare Axes span kare
fig.tight_layout() # Kyun? Figure ko apne saare Axes relayout karne deta hai taaki overlap na ho
Yeh step (axes[1,1]) kyun? 4 Axes ke saath koi sensible "current" nahi hota — index se address karna woh ambiguity hata deta hai jo pyplot resolve nahi kar sakta.
Worked example 3 — Pyplot aur OO same kaam karte hue
# pyplot (implicit) # OO (explicit)
plt.plot(x, y) fig, ax = plt.subplots()
plt.xlabel( "t" ) ax.plot(x, y)
plt.title( "T" ) ax.set_xlabel( "t" )
ax.set_title( "T" )
Same Line2D, same Text Artists — bas aap kisse baat karte hain woh alag hai. Method names note karo: pyplot xlabel use karta hai, OO set_xlabel use karta hai.
plt.plot do baar call kiya aur sab kuch ek hi plot pe aa gaya."
Kyun sahi lagta hai: aap chahte the do plots, toh aap assume karte ho do dikhenge.
Reality: pyplot same current Axes mein Artists add karta rehta hai jab tak aap naya Figure/Axes nahi banate.
Fix: explicitly naye Axes banao — fig2, ax2 = plt.subplots() — ya grid use karo.
Common mistake "Mera plot blank hai / kuch show nahi ho raha."
Kyun sahi lagta hai: code bina kisi error ke chala.
Reality: aapne fig, ax banaya lekin plt.plot se ek alag , hidden Axes pe plot kiya; ya aap plt.show()/savefig bhool gaye.
Fix: OO style mein hamesha apne ax pe plot karo, aur fig.savefig(...) ya plt.show() se end karo.
Common mistake "Mujhe Axes aur Axis mein confusion hoti hai."
Kyun sahi lagta hai: dono ki spelling almost same hai.
Reality: Axes = ek poora subplot ; Axis = uske andar x ya y ruler .
Fix: neeche wala mnemonic.
Common mistake "Labels galat subplot pe chale gaye."
Kyun sahi lagta hai: aapne plt.xlabel use kiya jab aap soch rahe the koi alag subplot current hai.
Fix: explicitly address karo: axes[0,1].set_xlabel(...).
Mnemonic FAA — jaise ek airport authority airspace control karti hai
F igure ⊃ A xes ⊃ A rtists.
Aur spelling trap ke liye: "Axe-S = ek S ubplot (ek Single plot); Axi-S = ek S ingle ruler." Plural-lagta Axes actually ek plot hai; singular-lagta Axis ek ruler hai.
Recall Feynman: ek 12-saal ke bacche ko samjhao
Ek scrapbook page imagine karo (woh hai Figure ). Page pe aap kuch photo frames chipkate ho (woh hain Axes — har frame ek choti si picture hai apne upar/neeche aur baaye/daaye rulers ke saath). Har frame ke andar aap cheezein draw karte ho: lines, dots, ek caption (woh hain Artists ). Matplotlib kabhi kabhi "woh frame jo maine aakhri baar touch kiya" yaad rakhta hai toh agar aap drawing karte raho, sab kuch usi frame mein pile ho jaata hai. Safe rehne ke liye, exactly woh frame point karo jo aap chahte ho — "frame number 3, yahan draw karo!" — na ki yeh umeed rakho ki usne sahi guess kar liya.
Teen nested container levels kaun se hain, outermost pehle?
Axes aur Axis mein kya fark hai?
plt.plot implicitly kya karta hai jo ax.plot explicit banata hai?
Matplotlib ka top-level container kaun sa hai jo ek saved image banta hai? Figure.
Matplotlib mein "Axes" ka matlab kya hai? Ek single plot/subplot apne x aur y coordinate system ke saath (NA ki do axis lines).
Matplotlib mein "Axis" (singular) kya hai? Ek direction ka scale, ticks aur tick-labels manage karne wala (jaise ax.xaxis).
Artist kya hai? Har drawable cheez ka base class: Line2D, Text, Patch, Spine, Tick, Legend, etc.
Outermost-to-innermost container hierarchy? Figure ⊃ Axes ⊃ Artist (Axis ke saath jo Axes ke andar per-direction manager hai).
plt.subplots(2,3) kya return karta hai? Ek Figure aur Axes objects ka 2×3 NumPy array.
Multi-panel figures ke liye OO interface preferred kyun hai? Yeh har Axes ko explicitly address karta hai (ax[r,c]) toh koi ambiguous "current" Axes nahi hota.
plt.plot(x,y) kis OO call ka shorthand hai? gca().plot(x,y) — current Axes pe plotting (agar koi nahi hai toh create hota hai).
savefig kis object pe call karte hain? Figure pe: fig.savefig("out.png").
OO style vs pyplot mein x-label kaun sa method set karta hai? OO: ax.set_xlabel(...); pyplot: plt.xlabel(...).
fig.suptitle kya karta hai? Poore Figure ke liye ek title add karta hai, jo uske saare Axes span karta hai.
Koi error nahi hone ke bawajood figure blank kyun dikh sakta hai? Aapne hidden current Axes pe plot kiya, apne ax pe nahi; ya plt.show()/savefig bhool gaye.
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)
Figure fig - whole canvas
Axes ax - one plot region
Artist - drawable base class
Line2D, Text, Spine, Tick
Pyplot interface plt.plot
OO interface fig, ax = plt.subplots