5.4.17 · Coding › Scientific Computing (Python)
Ek 3D surface plot basically ek height map hota hai: flat floor par har point ( x , y ) ke liye, tum ek height z = f ( x , y ) draw karte ho. Computer ko ek grid of floor coordinates chahiye, wo har grid point par height compute karta hai, phir un heights ke upar ek colored sheet (surface) ya lines ka mesh (wireframe) drapata hai. Poora trick yeh hai ki do 1D axis arrays ko ek 2D grid of coordinates mein badlo — yahi meshgrid karta hai.
Ek function z = f ( x , y ) do inputs leta hai. Isse plot karne ke liye tumhe f ko har x-value aur y-value ke har combination par evaluate karna padega. Agar tumhare paas x = [ x 0 , … , x m − 1 ] aur y = [ y 0 , … , y n − 1 ] hai, to tumhe z values ka ek n × m table chahiye: Z ij = f ( x j , y i ) .
==np.meshgrid== tumhare liye coordinates ka wo table banata hai. Do 1D arrays dene par yeh do 2D arrays X, Y return karta hai jo same shape ke hote hain, jahan har cell us grid location par x (ya y) coordinate hold karti hai.
Scratch se derivation (meshgrid haath se kya karta hai):
x = [1, 2, 3] # m = 3
y = [10, 20] # n = 2
X = [[1, 2, 3], # x repeated down each row
[1, 2, 3]]
Y = [[10,10,10], # y repeated across each column
[20,20,20]]
Toh cell (i,j) literally floor coordinate (X[i,j], Y[i,j]) hold karta hai. Ab Z = X**2 + Y sirf elementwise arithmetic hai → koi Python loops ki zaroorat nahi.
Ek 3D axes : ax = fig.add_subplot(projection='3d')
Coordinate grids X, Y aur heights Z (sabhi same 2D shape mein).
Ek surface call: ax.plot_surface(X, Y, Z) ya wireframe call: ax.plot_wireframe(X, Y, Z).
Surface vs Wireframe — kya farq hai?
plot_surface
plot_wireframe
Look
filled colored patches
sirf lines ka mesh
Color
cmap support karta hai (height→color)
usually single color=
Kab use karein
smooth solid shape dikhana ho, colored height dekhni ho
see-through, layers compare karni ho, lighter file
Key kwargs
cmap, rstride, cstride, edgecolor, alpha
color, rstride, cstride
rstride/cstride = "row stride / column stride" = har k-vi grid line lo. Kyun? Ek 200×200 grid mein 40,000 facets hote hain — dekhne ke liye bahut dense hai. Stride badhane se mesh patal ho jaata hai.
Worked example Example 1 — Ek paraboloid bowl
x = y = np.linspace( - 2 , 2 , 50 )
X, Y = np.meshgrid(x, y)
Z = X ** 2 + Y ** 2
ax.plot_surface(X, Y, Z, cmap = 'plasma' )
arange ki jagah linspace kyun? Hume exactly 50 evenly spaced points chahiye aur endpoints ka control chahiye — linspace(a, b, N) dono ends include karta hai. Yeh step kyun: smoother surfaces ke liye denser, even sampling zaroori hai.
Z = X**2 + Y**2 kyun? Yeh vectorized hai — poore 2D array par ek saath operate karta hai, seedha height table deta hai. Yeh step kyun: slow Python loops se bachata hai; shape automatically X se match ho jaata hai.
Result: ek bowl, origin par minimum z = 0 .
Worked example Example 2 — Same bowl ka wireframe, thina hua
ax.plot_wireframe(X, Y, Z, rstride = 5 , cstride = 5 , color = 'teal' )
rstride=5 kyun? 50×50 grid dono taraf 50 lines draw karta — bohot cluttered hota. Har 5vi line draw karne se ek clean see-through cage banta hai. Yeh step kyun: detail se zyada readability zaroori hai.
cmap kyun nahi? Wireframes lines hain, patches nahi, isliye color ek solid value hota hai. Yeh step kyun: lines per-face colormap carry nahi kar sakti.
Worked example Example 3 — Ek point par height padhna
Z = np.sin(np.sqrt(X**2 + Y**2)) ke liye, origin par height kya hai?
( 0 , 0 ) par: r = 0 = 0 , toh z = sin 0 = 0 . Yeh step kyun: coordinates ko f mein plug karo; plot sirf f ko drawn karta hai.
Door jahan r = π /2 hai: z = sin ( π /2 ) = 1 — pehla ripple crest. Yeh Forecast-then-Verify check hai: crest radius predict karo, phir figure mein dekho.
Common mistake "Main 1D x, y, z seedha plot_surface mein pass kar dunga."
Kyun sahi lagta hai: 2D mein, plt.plot(x, y) khushi se 1D arrays leta hai.
Fix: surfaces ko 2D X, Y, Z chahiye jo matching shape ke hon. Hamesha pehle X, Y = np.meshgrid(x, y) karo, phir Z = f(X, Y). 1D inputs shape error dete hain.
Common mistake Haath se banane ke baad Z shape mismatch.
Kyun sahi lagta hai: tumne Z ek loop se compute ki aur woh "theek lagti hai."
Fix: Z.shape exactly X.shape == Y.shape == (len(y), len(x)) hona chahiye. Agar tumne roles swap kiye, surface transposed/garbled aati hai. Plot karne se pehle .shape print karo.
projection='3d' bhool jaana.
Kyun sahi lagta hai: ordinary add_subplot() baaki sab ke liye kaam karta hai.
Fix: 3D methods (plot_surface) sirf ek 3D axes par exist karte hain. projection='3d' ke bina AttributeError milta hai.
np.meshgrid indexing ('xy' vs 'ij') mein confuse hona.
Kyun sahi lagta hai: dono 2D grids dete hain.
Fix: default indexing='xy' shape (len(y), len(x)) banata hai (plotting/images ke liye acha). 'ij' (len(x), len(y)) banata hai (matrix convention). Plots ke liye default rakhho.
Recall Feynman: ek 12-saal ke bachche ko explain karo
Socho floor par ek checkerboard hai. Har chote square par ek kisi height ki lakdi khadi karo. Agar tum sab lakdiyon ke tops par ek colored bedsheet failao, woh hai surface plot . Agar tum sirf lakdiyon ke tops ko strings se jodoge ek net banate hue, woh hai wireframe . meshgrid woh helper hai jo checkerboard par har square ki position figure out karta hai taaki computer ko pata chale har lakdi kahan jaati hai. Har lakdi ki height ek math rule z = f ( x , y ) se milti hai.
"GRID → HEIGHT → DRAPE"
G rid with meshgrid → H eight compute karo Z=f(X,Y) → D rape karo plot_surface / plot_wireframe se.
Aur args order ke liye: X, Y, Z — floor, floor, sky .
np.meshgrid(x, y) kya return karta hai aur shape kya hoti hai?Do 2D arrays X, Y, dono ki shape (len(y), len(x)), jo har grid point par x aur y coordinate dete hain.
plot_surface ke liye Z 2D kyun hona chahiye?Surface har (x,y) grid cell ke upar ek height draw karta hai, isliye use X,Y grids se matching heights ka poora 2D table chahiye.
3D axes kaise banate hain? ax = fig.add_subplot(projection='3d').
plot_surface aur plot_wireframe mein kya farq hai?Surface = filled colored patches (cmap support karta hai); wireframe = ek see-through mesh of lines (single color).
rstride aur cstride kya control karte hain?Row/column stride — kitni grid lines skip karni hain, ek bahut dense mesh ko patal karne ke liye.
plot_surface mein arguments ka order kya hai?(X, Y, Z) — pehle do floor-coordinate grids phir height grid.
Z = sin(sqrt(X**2+Y**2)) ke liye, origin par height kya hai?sin ( 0 ) = 0 .
projection='3d' bhoolne par kya error aata hai?AttributeError — plot_surface normal 2D axes par exist nahi karta.
Loop ki jagah vectorized Z = X**2 + Y kyun use karein? Yeh poore grid par ek saath elementwise operate karta hai — fast hai aur shape automatically X,Y se match ho jaati hai.
Plotting ke liye default meshgrid indexing kya hai? 'xy', jo shape (len(y), len(x)) deta hai.
meshgrid and broadcasting
2D plots — line, scatter
Matplotlib figure and axes objects
Contour plots (same Z grid, upar se dekha hua)
NumPy vectorization
Colormaps in Matplotlib