Step Plots, Stairs, and Stem Plots in Matplotlib

Draw values that hold and then jump with ax.step() and ax.stairs(), and show discrete samples with ax.stem().

Use ax.step() when a value stays put until it changes, such as a price or a plan limit: ax.step(x, y, where="post") holds each value until the next x. Use ax.stairs() when you have one value per interval and the interval edges, and ax.stem() for separate samples measured from a baseline.

Quick Example

A central bank's policy rate changes only on decision days, so a step plot shows it correctly:

import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

dates = pd.to_datetime([
    "2023-01-01", "2023-03-15", "2023-06-14", "2023-09-20", "2024-02-07",
    "2024-05-01", "2024-09-18", "2024-12-11", "2025-03-19", "2025-06-30",
])
rate = [2.50, 3.00, 3.50, 3.75, 3.75, 3.50, 3.00, 2.75, 2.50, 2.50]

fig, ax = plt.subplots(figsize=(9, 4.2), layout="constrained")
ax.step(dates, rate, where="post", color="#9b2226", linewidth=2.5)
ax.plot(dates[:-1], rate[:-1], "o", color="#9b2226")

ax.xaxis.set_major_formatter(mdates.ConciseDateFormatter(ax.xaxis.get_major_locator()))
ax.set_yticks(np.arange(2.5, 4.01, 0.25))
ax.yaxis.set_major_formatter("{x:.2f}%")
ax.set_title("Policy rate (illustrative data)")
ax.grid(alpha=0.3)
plt.show()

Step plot of an interest rate rising from 2.5 to 3.75 percent in 2023, then falling back to 2.5 percent by 2025

The last data point repeats the previous rate. With where="post", that final value only marks where the last step ends.

where="pre", "post", or "mid"

where decides which side of each point the step is on. The default is "pre", which is often not what you expect.

where Each y value covers Use for
"pre" (default) From the previous x up to this x Values recorded at the end of a period
"post" From this x up to the next x Values that take effect at this x
"mid" Halfway to the neighbors on both sides Values centered on each x
x = np.arange(1, 7)
y = np.array([3, 6, 4, 8, 5, 7])

fig, axs = plt.subplots(1, 3, figsize=(10, 3.4), sharey=True, layout="constrained")
for ax, where in zip(axs, ["pre", "post", "mid"]):
    ax.step(x, y, where=where, color="#005f73", linewidth=2.5)
    ax.plot(x, y, "o", color="#ee9b00", markersize=9)
    ax.set_title(f'where="{where}"')
plt.show()

Three step plots of the same six points showing the step placed before each point, after it, and centered on it

Fill Under a Step Plot

fill_between() has a matching step argument. Give it the same value as where:

hours = np.arange(25)
servers = np.array([4, 4, 3, 3, 3, 4, 6, 9, 12, 14, 14, 13, 12,
                    13, 14, 13, 12, 11, 10, 8, 7, 6, 5, 4, 4])

fig, ax = plt.subplots(figsize=(9, 4), layout="constrained")
ax.fill_between(hours, servers, step="post", color="#94d2bd", alpha=0.6)
ax.step(hours, servers, where="post", color="#005f73", linewidth=2)
ax.set_xlim(0, 24)
ax.set_ylim(0)
ax.set_xticks(range(0, 25, 3))
ax.set_xlabel("Hour of day")
ax.set_ylabel("Running servers")
plt.show()

Filled step plot of running servers by hour, low overnight and peaking at 14 in late morning and mid afternoon

ax.stairs() for Binned Values

ax.stairs() (Matplotlib 3.4+) takes n values and n + 1 edges, which is the shape np.histogram() returns. It avoids repeating the last value, and it handles intervals of different widths:

edges = [0, 10_000, 40_000, 90_000, 170_000, 215_000, 250_000]
rates = [10, 12, 22, 24, 32, 35]

fig, ax = plt.subplots(figsize=(9, 4), layout="constrained")
ax.stairs(rates, edges, fill=True, color="#e9d8a6")
ax.stairs(rates, edges, color="#bb3e03", linewidth=2)
ax.xaxis.set_major_formatter(lambda x, pos: f"${x / 1000:.0f}k")
ax.yaxis.set_major_formatter("{x:.0f}%")
ax.set_xlabel("Taxable income")
ax.set_title("Marginal rate by bracket (illustrative)")
plt.show()

Stairs chart of six tax brackets of unequal width, with the rate rising from 10 to 35 percent

Without fill=True, the outline drops to baseline=0 at both ends. Pass baseline=None to draw only the tops and the risers.

Stem Plots With ax.stem()

A stem plot draws a line from a baseline to each value, with a marker on top. It suits signals and other discrete sequences, where joining the points would suggest values in between:

lag = np.arange(0, 31)
response = np.exp(-lag / 9) * np.cos(2 * np.pi * lag / 10)

fig, ax = plt.subplots(figsize=(9, 4), layout="constrained")
markers, stems, baseline = ax.stem(lag, response)
markers.set(color="#ae2012", markersize=7)
stems.set(color="#ae2012", linewidth=1.5)
baseline.set(color="black", linewidth=1)
ax.set_xlabel("Sample")
ax.set_ylabel("Amplitude")
ax.set_title("Impulse response of a damped filter")
plt.show()

Stem plot of a damped oscillation over 30 samples, swinging above and below zero and shrinking

ax.stem() returns the markers, the stem lines, and the baseline, so you can style each one. The linefmt, markerfmt, and basefmt arguments take format strings such as "C1-" or "D". bottom= moves the baseline, and orientation="horizontal" turns the plot sideways.

For ranked categories, see lollipop charts.

step vs stairs vs plot

Your data Function
Change points: x and the value from then on ax.step(x, y, where="post")
Interval edges and one value per interval ax.stairs(values, edges)
Raw values to be binned ax.hist(data, histtype="step")
Cumulative distribution of raw values ax.ecdf(data) (Matplotlib 3.8+), see CDF plots
Separate samples from a baseline ax.stem(x, y)
A quantity that changes continuously ax.plot(x, y)

You can also get steps from ax.plot() with drawstyle="steps-post".

Similar Topics