5.4.16 · HinglishScientific Computing (Python)

2D plots — line, scatter, bar, histogram, contour, imshow

2,039 words9 min readRead in English

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 hai

6. 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)

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow


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?
Jab points independent hon (koi meaningful order/connection nahi), ya extra variables ko point color/size ke zariye encode karna ho.
Line plot smooth lagta hai — actually usse smooth kya banata hai?
Bahut saare closely-spaced sample points; ye consecutive points ke beech straight segments hi hote hain.
Bar chart aur histogram mein kya fark hai?
Bar = aapke diye har category ka ek bar; histogram = bars auto-computed bins hain jo count karte hain ki kitne samples har value-range mein aate hain.
B equal bins ke liye histogram bin width formula kya hai?
.
hist mein density=True kya karta hai aur kyun?
Heights ko scale karta hai taaki total area = 1 ho (PDF estimate); har height ban jaati hai.
Level c par contour line mathematically kya hai?
Points ka set .
contour se pehle np.meshgrid kyun call karna padta hai?
Contour ko Z ki zaroorat hai 2D grid ke roop mein har (x,y) pair ke upar; meshgrid X, Y coordinate tables banata hai.
imshow kya dikhata hai aur rows/cols kaise mapped hote hain?
Ek 2D array ko colored cells ke roop mein; rows = y, columns = x; value → color.
imshow image ulti kyun dikh sakti hai aur fix kya hai?
Default 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?
Object-oriented API explicit control deta hai, multiple subplots ke saath zaroori hai.
imshow mein extent kya karta hai?
Array indices ko real coordinate ranges [xmin,xmax,ymin,ymax] mein map karta hai.
Koi bhi plot banane ka 5-step ritual kya hai?
fig,ax = subplots; ax.(...); labels set karo; legend/colorbar; plt.show().

Connections

Concept Map

mapped to pixels

contains

every method is ax.something

ordered x connected

independent x,y pairs

categories

one sample array

scalar field z=f x,y

2D array as cells

draws segments between points

many points

80/20 core

80/20 core

80/20 core

Data numbers

Matplotlib plot

Figure canvas

Axes coordinate system

Line plot

Scatter

Bar

Histogram

Contour

imshow

Dot-to-dot segments

Illusion of curve

Most used trio