Matplotlib 3D

Quick 3D Plots with Matplotlib

Matplotlib's mpl_toolkits.mplot3d module provides basic 3D plotting: surfaces, wireframes, scatter, and contour plots. It is not a true 3D renderer β€” it projects 3D objects onto 2D using painter's algorithm β€” but for quick exploratory visualization and journal figures, it is often sufficient.

Definition:

Creating 3D Axes

Add projection='3d' to create a 3D Axes:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
# or
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})

Key methods: ax.plot_surface(), ax.plot_wireframe(), ax.scatter3D(), ax.plot3D(), ax.contour3D().

The 3D axes support ax.view_init(elev, azim) to set the camera angle.

Definition:

Surface Plots

ax.plot_surface(X, Y, Z, cmap, **kwargs) renders a 3D surface from meshgrid arrays:

x = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, x)
Z = np.sinc(np.sqrt(X**2 + Y**2))

fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
surf = ax.plot_surface(X, Y, Z, cmap='viridis',
                       alpha=0.8, edgecolor='none')
fig.colorbar(surf, shrink=0.6)

Definition:

Wireframe and 3D Scatter

Wireframe shows the mesh structure without filling:

ax.plot_wireframe(X, Y, Z, color='blue', linewidth=0.5,
                  rstride=5, cstride=5)

3D Scatter places points in 3D space:

ax.scatter3D(xs, ys, zs, c=colors, s=sizes, cmap='viridis')

Theorem: Matplotlib 3D Limitations

Matplotlib's 3D rendering uses a 2D projection with painter's algorithm (back-to-front sorting). This means:

  1. No true depth testing β€” intersecting surfaces render incorrectly.
  2. No lighting β€” surfaces are flat-shaded by colormap only.
  3. Performance β€” degrades rapidly beyond ~10410^4 polygons.

For interactive 3D, volume rendering, or complex scenes, use PyVista (Section 17.2) or Plotly 3D.

Think of Matplotlib 3D as "2.5D" β€” it is a convenient approximation for simple surfaces and scatter plots, not a full 3D engine.

Example: Visualizing a Cost Function Surface

Plot the Rosenbrock function f(x,y)=(1βˆ’x)2+100(yβˆ’x2)2f(x,y) = (1-x)^2 + 100(y-x^2)^2 as a 3D surface with the minimum marked.

Example: 3D Scatter: MIMO Capacity vs Parameters

Plot MIMO capacity as a function of SNR and number of antennas as a 3D scatter plot with color encoding.

Example: Surface with Projected Contours

Show a 3D surface with contour lines projected onto the floor.

3D Surface Explorer

Explore different mathematical surfaces in 3D with adjustable viewing angles and colormaps.

Parameters

Common Mistake: Matplotlib 3D Depth Sorting Artifacts

Mistake:

Plotting two intersecting surfaces in Matplotlib 3D produces visual artifacts where one surface incorrectly occludes the other.

Correction:

For intersecting geometry, switch to PyVista or Plotly 3D which have proper depth testing. Alternatively, increase alpha transparency on both surfaces.

Key Takeaway

Use Matplotlib 3D for simple surfaces and journal figures. It produces publication-quality vector output and integrates with your existing Matplotlib workflow. For interactive exploration or complex 3D scenes, switch to PyVista or Plotly.

Quick Check

What does ax.view_init(elev=45, azim=-60) do?

Sets the figure size

Sets the 3D camera elevation and azimuth angles

Initializes the animation

Sets the axis limits

Surface Plot

A 3D visualization that renders a function z=f(x,y) as a colored mesh of quadrilaterals.

Wireframe Plot

A 3D visualization showing only the mesh edges without filled surfaces, useful for seeing through the geometry.

Historical Note: Matplotlib 3D History

2007

The mplot3d toolkit was contributed by John Porter in 2007 as an add-on to Matplotlib. Despite its limitations (no true depth buffer), it remained the default Python 3D plotting tool for over a decade because of its tight integration with Matplotlib's ecosystem. In the 2020s, PyVista and Plotly have emerged as superior alternatives for interactive 3D work.