Stem Plots with stem(x, y)ΒΆ

A stem plot displays each data point as a marker on top of a vertical line extending from a baseline (typically zero), making it easy to see both the value and its deviation from the baseline at a glance. The Axes.stem() method draws these β€œlollipop” markers at each (x, y) position.

Why this matters for data science: Stem plots are ideal for discrete data where you want to emphasize individual values rather than trends between them. They are commonly used for visualizing discrete probability distributions, impulse responses in signal processing, feature coefficients from linear models, or any dataset where the spacing between points carries meaning. Compared to bar charts, stem plots are less visually cluttered when you have many data points, since the thin lines take up less ink than full bars. Compared to scatter plots, the vertical lines make it easier to judge the magnitude of each value relative to the baseline. The example uses 0.5 + np.arange(8) for x-positions, centering the stems between integer tick values.

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('_mpl-gallery')

# make data
x = 0.5 + np.arange(8)
y = [4.8, 5.5, 3.5, 4.6, 6.5, 6.6, 2.6, 3.0]

# plot
fig, ax = plt.subplots()

ax.stem(x, y)

ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
       ylim=(0, 8), yticks=np.arange(1, 8))

plt.show()