5.4.17Scientific Computing (Python)

3D plots — surface, wireframe

1,775 words8 min readdifficulty · medium1 backlinks

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.

Figure — 3D plots — surface, wireframe

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 z=f(x,y)z = f(x,y).


Active Recall

What does np.meshgrid(x, y) return and what shape?
Two 2D arrays 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?
Surface draws a height over every (x,y) grid cell, so it needs a full 2D table of heights matching the X,Y grids.
How do you create a 3D axes?
ax = fig.add_subplot(projection='3d').
Difference between plot_surface and plot_wireframe?
Surface = filled colored patches (supports cmap); wireframe = a see-through mesh of lines (single color).
What do rstride and cstride control?
Row/column stride — how many grid lines to skip, thinning a too-dense mesh.
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?
sin(0)=0\sin(0)=0.
What error if you forget projection='3d'?
AttributeError — plot_surface doesn't exist on a normal 2D axes.
Why use vectorized Z = X**2 + Y instead of a loop?
It operates elementwise on the whole grid at once — fast and the shape auto-matches X,Y.
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

requires

input to

input to

returns 2D

elementwise f

drape sheet

drape mesh

hosts

hosts

colored by

thin facets

thin facets

z = f x,y needs 2 inputs

Grid of x,y coordinates

1D array x len m

np.meshgrid

1D array y len n

X, Y shape n,m

Z heights same shape

ax.plot_surface

ax.plot_wireframe

3D axes projection=3d

cmap height to color

rstride / cstride

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, 3D surface plot basically ek height map hai. Har (x,y)(x, y) point pe ek height z=f(x,y)z = f(x,y) 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.

Go deeper — visual, from zero

Test yourself — Scientific Computing (Python)

Connections