3D plots — surface, wireframe
WHY do we need a grid at all?
==np.meshgrid== builds that table of coordinates for you. Given two 1D arrays it returns two 2D arrays X, Y of the same shape, where each cell holds the x (or y) coordinate at that grid location.
Derivation from scratch (what meshgrid does by hand):
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]]
So cell (i,j) literally holds the floor coordinate (X[i,j], Y[i,j]). Now Z = X**2 + Y is just elementwise arithmetic → no Python loops needed.
HOW to make the plot
Surface vs Wireframe — what's the difference?
plot_surface |
plot_wireframe |
|
|---|---|---|
| Look | filled colored patches | just a mesh of lines |
| Color | supports cmap (height→color) |
usually single color= |
| When | show a smooth solid shape, see colored height | see-through, compare layers, lighter file |
| Key kwargs | cmap, rstride, cstride, edgecolor, alpha |
color, rstride, cstride |
rstride/cstride = "row stride / column stride" = take every k-th grid line. Why? A 200×200 grid has 40 000 facets — too dense to see. Increasing stride thins the mesh.

Worked Examples
Common Mistakes
Recall Feynman: explain to a 12-year-old
Imagine a checkerboard on the floor. At every little square you stick a pole of some height. If you stretch a colored bedsheet over all the pole tops, that's a surface plot. If instead you just connect the pole tops with strings making a net, that's a wireframe. meshgrid is the helper that figures out the position of every square on the checkerboard so the computer knows where each pole goes. The height of each pole is given by a math rule .
Active Recall
What does np.meshgrid(x, y) return and what shape?
X, Y, each of shape (len(y), len(x)), giving the x and y coordinate at every grid point.Why must Z be 2D for plot_surface?
How do you create a 3D axes?
ax = fig.add_subplot(projection='3d').Difference between plot_surface and plot_wireframe?
What do rstride and cstride control?
Order of arguments to plot_surface?
(X, Y, Z) — the two floor-coordinate grids then the height grid.For Z = sin(sqrt(X**2+Y**2)), the height at the origin is?
What error if you forget projection='3d'?
plot_surface doesn't exist on a normal 2D axes.Why use vectorized Z = X**2 + Y instead of a loop?
Default meshgrid indexing for plotting?
'xy', giving shape (len(y), len(x)).Connections
- meshgrid and broadcasting
- 2D plots — line, scatter
- Matplotlib figure and axes objects
- Contour plots (same Z grid, viewed from above)
- NumPy vectorization
- Colormaps in Matplotlib
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, 3D surface plot basically ek height map hai. Har point pe ek height hoti hai. Computer ko pehle ek grid chahiye — yani floor ke saare points ka table. Yeh kaam np.meshgrid(x, y) karta hai: do 1D arrays leke do 2D arrays X, Y banata hai, jिनmein har cell pe uss point ka x aur y coordinate hota hai. Shape hoti hai (len(y), len(x)). Phir Z = f(X, Y) ek hi line mein, vectorized, poora height table de deta hai — koi loop nahi.
Plot banana simple hai: ax = fig.add_subplot(projection='3d') se 3D axes banao (yeh projection='3d' zaroori hai, warna plot_surface exist hi nahi karta), phir ax.plot_surface(X, Y, Z, cmap='viridis') ya ax.plot_wireframe(X, Y, Z). Surface = bhari hui colored chaadar, cmap se height color batata hai. Wireframe = sirf lines ka jaal, see-through, halka. Agar grid bahut dense hai to rstride/cstride badha do — yeh har k-th line draw karta hai, taaki saaf dिखe.
Sabse common galti: 1D x, y, z seedha plot_surface mein de dena. Yaad rakho — surface ko 2D X, Y, Z chahiye, sab same shape. Hamesha pehle meshgrid karo, phir Z compute karo, aur plot se pehle Z.shape check kar lo. Mantra yaad rakho: GRID → HEIGHT → DRAPE. Yeh matter karta hai kyunki saare scientific visualizations — temperature maps, optimization surfaces, potential fields — isi pattern pe bante hain.