3D Stem Plots with stem(x, y, z)ΒΆ
A 3D stem plot displays data points as markers at the tips of vertical lines (βstemsβ) that drop down to a baseline plane, making it easy to read the exact height of each point while seeing its position in the x-y plane. The Axes3D.stem() method takes three coordinate arrays and draws a line from each (x, y) position on the floor up to the corresponding z value.
Why this matters for data science: Stem plots are particularly effective when you need to show discrete measurements at specific locations β for example, sensor readings at known positions, sampled signal amplitudes at discrete time-frequency points, or feature importance scores arranged spatially. Compared to a scatter plot, the vertical lines provide a strong visual anchor that makes it easier to judge relative heights. The example arranges points along a parametric curve using sine and cosine to create a circular pattern in the x-y plane, with z increasing linearly β demonstrating how stem plots can reveal the structure of sampled data along a path.
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# Make data
n = 20
x = np.sin(np.linspace(0, 2*np.pi, n))
y = np.cos(np.linspace(0, 2*np.pi, n))
z = np.linspace(0, 1, n)
# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.stem(x, y, z)
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
plt.show()