Data VisualizationIntermediate

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.

Loading...

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:

  • Mathematical surfaces β€” visualizing functions like z = f(x, y)
  • Scientific data β€” temperature/pressure/altitude, terrain elevation
  • Machine learning β€” clustering visualized in feature space
  • Engineering β€” parametric curves, signal trajectories, motion paths
  • 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:

    MethodWhat 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 map

    Now 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

  • Pick a good colormap: 'viridis', 'plasma', 'coolwarm', and 'terrain' all look great on surfaces.
  • Use `alpha=0.7` on scatter points so overlapping clusters stay readable.
  • Reduce mesh density with rstride / cstride if your wireframe looks too busy.
  • Add a colorbar with fig.colorbar(surf) β€” it shows what each color means.
  • Set viewing angle: ax.view_init(elev=30, azim=45) rotates the camera (great for screenshots).
  • When to Use Each Plot Type

  • Surface β†’ smooth mathematical functions, terrain, heatmaps that need depth
  • Wireframe β†’ showing structure without hiding the back of the surface
  • Scatter β†’ discrete data points, ML clusters, sensor readings
  • Line β†’ trajectories, parametric curves, time-evolving paths
  • 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