Stem plots in Matplotlib

By Martin McBride, 2022-07-09
Tags: matplotlib stem plot
Categories: matplotlib numpy

A stem plot is quite similar to a bar chart. The difference is that, instead of using a bar for each data point, a stem plot uses a vertical line with a marker at the top.

Here is an example stem plot:

This is very similar to the bar chart we created earlier.

Stem plots can be useful if there are a lot of data points. Since the line and marker occupy less area than the bar, the plot looks less cluttered. This is largely a matter of personal taste.

The code is very similar:

import matplotlib.pyplot as plt
import csv

with open("2009-temp-monthly.csv") as csv_file:
    csv_reader = csv.reader(csv_file, quoting=csv.QUOTE_NONNUMERIC)
    temperature = [x[0] for x in csv_reader]

month_names = ["J", "F", "M", "A", "M", "J",
               "J", "A", "S", "O", "N", "D"]

months = range(12)

plt.title("Monthly temperature 2009")
plt.xticks(months, month_names)

plt.stem(months, temperature)

This code is available on github as

The only difference is the call to plt.stem rather than

Styling stem plots

We can style a stem plot in a similar way to a line plot or scatter plot, with the following optional parameters:

  • The fmt parameter uses a string to specify basic colour, line style, and marker shape options. Use this for simple formatting.
  • The color parameter sets the line colour, using named colours of RGB values.
  • The linewidth parameter sets the width of the line.
  • The linestyle parameter can create dashed lines of various types.
  • The marker, markeredgecolor, markeredgewidth, markerfacecolor, and markersize control the marker appearance.

These options are covered in more detail in the article line and marker styles.

See also

If you found this article useful, you might be interested in the book NumPy Recipes or other books by the same author.

Popular tags

2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop formula function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart polygon positional parameter print product programming paradigms programming techniques pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest