2D plots — line, scatter, bar, histogram, contour, imshow
5.4.16· Coding › Scientific Computing (Python)
YE CHHE KYU EXIST KARTE HAIN (decision tree)
KYA cheez inhe alag karti hai: ye aapke data ke baare mein kya assume karte hain.
| Plot | Data shape | Implied meaning | Key question |
|---|---|---|---|
plot |
y vs ordered x | points connected hain (ek function/series) | "trend?" |
scatter |
(x, y) pairs | points independent hain | "correlation/cluster?" |
bar |
category → value | discrete groups | "sabse bada kaunsa hai?" |
hist |
samples ka ek array | value ranges ki frequency | "distribution kaisi hai?" |
contour |
grid Z[i,j]=f(x,y) | ek height map (level curves) | "surface ki shape?" |
imshow |
2D array of values | colored pixel grid | "matrix/image khud?" |
1. Line plot — ax.plot
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 200) # 200 pts -> smooth
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y, color="tab:blue", lw=2, label="sin x")
ax.set_xlabel("x (rad)"); ax.set_ylabel("sin x")
ax.legend(); ax.grid(True)
plt.show()2. Scatter — ax.scatter
rng = np.random.default_rng(0)
x = rng.normal(size=100)
y = 2*x + rng.normal(size=100) # correlated
ax.scatter(x, y, s=30, c=x, cmap="viridis", alpha=0.7)3. Bar — ax.bar
labels = ["A", "B", "C"]; vals = [5, 9, 3]
ax.bar(labels, vals, width=0.6, color="tab:orange")Categorical x KYU: Matplotlib category ko integer position par rakhta hai aur uske neeche label print karta hai.
4. Histogram — ax.hist
data = rng.normal(loc=0, scale=1, size=5000)
ax.hist(data, bins=40, density=True, color="tab:green", alpha=0.8)5. Contour — ax.contour / ax.contourf
xs = np.linspace(-3, 3, 100); ys = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(xs, ys)
Z = np.exp(-(X**2 + Y**2)) # a Gaussian bump
cs = ax.contour(X, Y, Z, levels=8, cmap="plasma")
ax.clabel(cs, inline=True) # label each level
# ax.contourf(...) lines ki jagah levels ke beech color fill karta hai6. imshow — ax.imshow
ax.imshow(Z, origin="lower", extent=[-3,3,-3,3], cmap="inferno", aspect="auto")
fig.colorbar(ax.images[0], ax=ax, label="f(x,y)")Sab kuch ek saath (cheat figure)

Recall Feynman: ek 12-saal ke bacche ko samjhao
Graph paper ke baare mein socho. Line: main kuch dots plot karta hoon aur unhe join-the-dots puzzle ki tarah connect karta hoon — achha hai "din bhar temperature kaise badla" ke liye. Scatter: main sirf dots bikherta hoon, koi lines nahi — achha hai "kya lamba baccha zyada bhari hota hai?" ke liye. Bar: har cheez ke liye lambe blocks — "kisne sabse zyada goals kiye?" Histogram: main sabki height ko boxes mein sort karta hoon (140-150cm, 150-160cm...) aur count karta hoon har box mein kitne hain — group ki shape dikhata hai. Contour: ek treasure map jaisa jo rings se hills dikhata hai. imshow: main graph paper ke har chhote square ko uske number ke hisaab se color karta hoon, jaise paint-by-numbers — isi tarah photo ya heatmap kaam karta hai.
Flashcards
Line ki jagah scatter kab use karte hain?
Line plot smooth lagta hai — actually usse smooth kya banata hai?
Bar chart aur histogram mein kya fark hai?
B equal bins ke liye histogram bin width formula kya hai?
hist mein density=True kya karta hai aur kyun?
Level c par contour line mathematically kya hai?
contour se pehle np.meshgrid kyun call karna padta hai?
imshow kya dikhata hai aur rows/cols kaise mapped hote hain?
imshow image ulti kyun dikh sakti hai aur fix kya hai?
origin="upper" row 0 ko top par rakhta hai; math orientation ke liye origin="lower" use karo.ax.plot(...) ko plt.plot(...) par prefer kyun karte hain?
imshow mein extent kya karta hai?
Koi bhi plot banane ka 5-step ritual kya hai?
Connections
- Matplotlib Figure vs Axes object model
- NumPy meshgrid and broadcasting
- Probability density functions and normalization
- Colormaps and perceptual uniformity (viridis)
- 3D surface plots (plot_surface)
- Seaborn — statistical plotting wrappers
- Image arrays and pixel coordinates