Matplotlib – Step Plot

To draw a Step Plot using Matplotlib, call matplotlib.pyplot.step() function, and pass required values: x-axis data and y-axis data.

The definition of matplotlib.pyplot.step() function is

</>
Copy
step(x, y, [fmt], *, data=None, where='pre', **kwargs)
step(x, y, [fmt], x2, y2, [fmt2], ..., *, where='pre', **kwargs)

Example

In the following program, we will take some same data points and draw a step plot.

The data for X-axis of step plot is [0, 1, 2, 3, 4, 5, 6, 7, 8].

The data for Y-axis of step plot is [3, 4, 8, 2, 6, 5, 9, 4, 2].

example.py

</>
Copy
import matplotlib.pyplot as plt

# make data
x = [0, 1, 2, 3, 4, 5, 6, 7, 8]
y = [3, 4, 8, 2, 6, 5, 9, 4, 2]

#draw step plot
plt.step(x, y)

#show plot as image
plt.show()

Output

Matplotlib Step Plot

Conclusion

In this Matplotlib Tutorial, we learned how to draw a Step Plot using Matplotlib PyPlot API.