Matplotlib 3D Plot in Python
Create stunning 3D plots in Python with Matplotlib! Learn 3D surface, scatter, wireframe, and contour plots with runnable code β no setup required.
Try it yourself
Run this code directly in your browser. Click "Open in full editor" to experiment further.
Click Run to see output
Or press Ctrl + Enter
How it works
Matplotlib 3D plots turn boring tables of (x, y, z) numbers into visualizations you can actually understand at a glance. π
Why 3D Plots?
When your data has three variables that all relate to each other, a flat 2D chart can't show the whole story. 3D plots are perfect for:
z = f(x, y)The Magic Line: `projection='3d'`
Matplotlib's 3D support lives inside mpl_toolkits.mplot3d, but you don't need to import it directly in modern Matplotlib. Just write:
ax = fig.add_subplot(111, projection='3d')That single keyword argument flips the axes from flat 2D to a full 3D coordinate system. From there, you have a handful of go-to methods:
| Method | What it draws |
|---|---|
ax.plot_surface(X, Y, Z) | A solid colored surface (the "mountain") |
ax.plot_wireframe(X, Y, Z) | A see-through mesh of lines |
ax.scatter(x, y, z) | 3D dots β great for clusters or point clouds |
ax.plot(x, y, z) | A continuous 3D line β helices, trajectories |
ax.contour3D(X, Y, Z) | Stacked contour rings, like a topographic map |
The `meshgrid` Trick
For surfaces and wireframes, you don't pass simple lists β you need a 2D grid of every (x, y) coordinate pair. That's what np.meshgrid does:
x = np.linspace(-5, 5, 50) # 50 x-values
y = np.linspace(-5, 5, 50) # 50 y-values
X, Y = np.meshgrid(x, y) # Two 50Γ50 matrices
Z = some_function(X, Y) # A 50Γ50 height mapNow Z[i][j] is the height above the point (X[i][j], Y[i][j]). Matplotlib stitches them into a smooth surface.
Step-by-Step: Build a Surface Plot
1. Pick a function Z = f(X, Y) (or load real data shaped as a 2D array).
2. Create X, Y with np.meshgrid(...).
3. Compute Z from X and Y.
4. Make a figure: fig = plt.figure().
5. Add a 3D axis: ax = fig.add_subplot(111, projection='3d').
6. Draw: ax.plot_surface(X, Y, Z, cmap='viridis').
7. Label the axes and add a colorbar.
8. Call plt.show().
Pro Tips
'viridis', 'plasma', 'coolwarm', and 'terrain' all look great on surfaces.rstride / cstride if your wireframe looks too busy.fig.colorbar(surf) β it shows what each color means.ax.view_init(elev=30, azim=45) rotates the camera (great for screenshots).When to Use Each Plot Type
Run the snippet above to see all four in action β you'll get a sinc surface, a 3-cluster scatter, a cos(x)Β·sin(y) wireframe, and a rising helix. β¨
Related examples
Data Visualization with Matplotlib
Learn data visualization in Python with Matplotlib! A fun guide to creating line plots, scatter plots, and bar charts the recommended object-oriented way.
NumPy Array Operations in Python
Learn NumPy basics in Python! A fun and easy guide to super-fast arrays, matrices, and data science math without using slow for-loops.
Data Analysis with Pandas in Python
Learn Pandas for data analysis in Python! A beginner-friendly guide to DataFrames, filtering data, grouping, and handling messy missing data just like Excel.