3D Plots

Matplotlib is a Python library for creating charts and visualizations — and with its 3D toolkit it can draw surfaces and scatter points across three axes, not just two.

Learn 3D Plots in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll create a 3D Axes, plot a colorful surface from a coordinate grid, and scatter points in space with x, y, and z labels.

A 3D plot lives on a special Axes. You ask for one by passing projection='3d' when you add the subplot. That single argument unlocks methods like scatter and plot in three dimensions.

What you'll see: a cube-shaped plot box with 30 dots floating inside it at random x, y, and z positions. Because it's a 3D Axes, you can rotate it with the mouse in an interactive window, and all three axes are clearly labeled X, Y, and Z.

A surface is a height ( z ) at every point on an x-y grid. np.meshgrid builds that grid for you, and ax.plot_surface drapes a colored sheet over it.

What you'll see: a smooth, rippling surface that looks like waves spreading out from the center — the classic "sombrero" shape of sin(sqrt(x² + y²)) . The viridis colormap shades high peaks yellow and low troughs purple.

The same ax.plot you know from 2D works in 3D too — just pass a third list of z values. A parametric curve like a helix is a perfect example.

What you'll see: a spring-like spiral coiling upward. As t grows, x and y trace a circle while z keeps rising, so the line winds around and climbs — exactly like a stretched-out helix.

Replace each ___ to create a 3D Axes and scatter points.

❌ AttributeError: 'Axes' object has no attribute 'set_zlabel'

You forgot projection='3d' . A plain 2D Axes has no z-axis, so 3D methods don't exist on it.

X, Y, and Z must all be 2D and the same shape. Build X and Y with np.meshgrid , then compute Z from them.

Build a bowl-shaped surface from z = x² + y² and label all three axes.

Lesson 18 complete — your charts now have depth!

You created 3D Axes, scattered points in space, drew surfaces from a meshgrid, plotted a helix, and labeled all three axes.

🚀 Up next: Customizing with rcParams — set global style defaults for every chart.

Practice quiz

How do you create a 3D Axes when adding a subplot?

  • fig.add_subplot(111, kind='3d')
  • fig.add_subplot(111, projection='3d')
  • fig.add_3d_subplot(111)
  • fig.add_subplot(111, dim=3)

Answer: fig.add_subplot(111, projection='3d'). Pass projection='3d' to add_subplot to get a 3D Axes.

In modern Matplotlib, do you still need to import Axes3D?

  • Yes, always
  • Only on Linux
  • No — projection='3d' is enough on its own
  • Only for scatter plots

Answer: No — projection='3d' is enough on its own. projection='3d' alone works; importing Axes3D is no longer required.

Which method labels the z-axis?

  • ax.set_xlabel()
  • ax.set_ylabel()
  • ax.label_z()
  • ax.set_zlabel()

Answer: ax.set_zlabel(). ax.set_zlabel() names the z-axis on a 3D Axes.

What does np.meshgrid(x, y) produce for a surface plot?

  • Two 2D coordinate grids X and Y
  • A single flat list
  • A 3D array of z values
  • A list of colors

Answer: Two 2D coordinate grids X and Y. meshgrid expands two 1D arrays into 2D coordinate grids X and Y.

Which method draws a colored surface over an x-y grid?

  • ax.surface()
  • ax.plot_surface(X, Y, Z)
  • ax.fill_3d(X, Y, Z)
  • ax.mesh(X, Y, Z)

Answer: ax.plot_surface(X, Y, Z). ax.plot_surface(X, Y, Z) drapes a colored sheet over the grid.

How do you scatter points in 3D space?

  • ax.points3d(x, y, z)
  • ax.dots(x, y, z)
  • ax.scatter(x, y, z)
  • ax.scatter3d(x, y, z)

Answer: ax.scatter(x, y, z). ax.scatter(x, y, z) plots points in three dimensions on a 3D Axes.

How do you draw a 3D line such as a helix?

  • ax.line3d(x, y, z)
  • ax.curve(x, y, z)
  • ax.plot3d(x, y, z)
  • ax.plot(x, y, z)

Answer: ax.plot(x, y, z). The familiar ax.plot also works in 3D — just pass a third z argument.

What requirement must X, Y, and Z meet for plot_surface?

  • All 2D and the same shape
  • All 1D arrays
  • Z must be a scalar
  • X and Y must be lists of strings

Answer: All 2D and the same shape. X, Y, and Z must all be 2D and the same shape, built via meshgrid.

What error means you forgot projection='3d'?

  • ValueError: bad projection
  • ImportError: no mplot3d
  • AttributeError: 'Axes' object has no attribute 'set_zlabel'
  • KeyError: '3d'

Answer: AttributeError: 'Axes' object has no attribute 'set_zlabel'. A plain 2D Axes has no z-axis, so set_zlabel does not exist on it.

What argument adds a colormap to a surface plot?

  • color='viridis'
  • palette='viridis'
  • shade='viridis'
  • cmap='viridis'

Answer: cmap='viridis'. cmap='viridis' shades the surface by height with the viridis colormap.