Matplotlib – Stem Plot

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

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

</>
Copy
stem([locs,] heads, linefmt=None, markerfmt=None, basefmt=None)

stem() returns a container that could be treated like a tuple (markerline, stemlines, baseline).

Example

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

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

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

example.py

</>
Copy
import matplotlib.pyplot as plt

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

#draw step plot
plt.stem(locs, heads)

#show plot as image
plt.show()

Output

Matplotlib - Stem Plot

Conclusion

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