Python:Matplotlib step()
Published Nov 19, 2025
Contribute to Docs
step() creates a piecewise-constant (step) plot from 1-D data. Each sample in y is represented as a horizontal segment and adjacent samples are connected by vertical lines. The where parameter controls whether the step change happens before, after, or at the midpoint of the x-coordinate.
Syntax
matplotlib.pyplot.step(x, y, *args, where='pre', **kwargs)
Parameters:
x(array-like): 1-D sequence of x positions. It is assumed (but not enforced) thatxis increasing.y(array-like): Sequence of y-values. Must have the same length asx.*args: positional arguments forwarded toplot()(for example a format string such as'o-').where({'pre','post','mid'}, optional) — controls the placement of the steps:'pre'(default): the y value is constant to the left of each x position; the interval(x[i-1], x[i]]has valuey[i].'post': the y value is constant to the right of each x position; the interval[x[i], x[i+1])has valuey[i].'mid': each step is centered between neighbors; level changes occur at midpoints between successive x values.
**kwargs: any other keyword arguments accepted bymatplotlib.pyplot.plot, such aslabel,linewidth,linestyle,alpha, etc.
Returns:
A list of matplotlib.lines.Line2D objects representing the plotted steps.
Notes:
- Use
plt.stairs()if you have explicit step edges (left/right boundaries) rather than sample positions.step()is a thin wrapper aroundplot()and supports mostplotformatting options.
Example
The example below draws three three-step series using where='pre', where='mid', and where='post'.
import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 5, 6)y = np.array([0, 1, 0, 1, 0, 1])plt.figure(figsize=(6, 3))plt.step(x, y + 2, where='pre', label="pre")plt.step(x, y + 1, where='mid', label="mid")plt.step(x, y + 0, where='post', label="post")plt.ylim(-0.5, 3.5)plt.xlabel('x')plt.ylabel('y')plt.title('Example: matplotlib.pyplot.step')plt.legend()plt.grid(True)plt.show()
The output of this code will be:

Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Matplotlib on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours