How to Create a Stem-and-Leaf Plot in Python


A stem-and-leaf plot is a chart that displays data by splitting up each value in a dataset into a stem and a leaf. It’s a unique plot because it helps us visualize the shape of a distribution while still displaying the raw individual data values.

This tutorial explains how to create a stem-and-leaf plot in Python.

Example: Stem-and-Leaf Plot in Python

Suppose we have the following dataset in Python:

x = [32, 34, 35, 41, 44, 46, 47, 52, 52, 53, 56, 61, 62]

To create a stem-and-leaf plot for this dataset, we can use the stemgraphic library:

pip install stemgraphic

Once this is installed, we can use the following code to create a stem-and-leaf plot for our dataset:

import stemgraphic

#create stem-and-leaf plot
fig, ax = stemgraphic.stem_graphic(x)

Stem-and-leaf plot in Python

The way to interpret this plot is as follows:

  • The number in the red box at the bottom of the plot displays the minimum number in the dataset (32).
  • The number in the red box at the top of the plot displays the maximum number in the dataset (62).
  • The numbers in the far left display the aggregated count of values in the plot. For example, the first row contains 2 aggregated values, the second row contains 3 aggregated values, the third row contains 5 aggregated values, and so on.
  • The numbers in the middle column display the stems, which are 345, and 6.
  • The numbers in the far right column display the leaves.

This single plot provides us with a ton of information about the distribution of values in this dataset.

Additional Resources

An Introduction to Stem-and-Leaf Plots
Stem-and-Leaf Plot Generator

Leave a Reply

Your email address will not be published.