Login
📚 Python for Introductory Statistics
Chapters ▾

5.6 Stem and leaf Plots

We need to install Stemgraphic for plotting stem-and-leaf plot. This is because Stemgraphic is not included in any of the Python Libraries. To install Stemgraphic type the following syntax. Stemgraphic Modules Link

Note: Stemplots (Stem-and-Leaf plots) should only be used for small datasets.

pip install stemgraphic

The output generated by the installation process has been deleted above. It is not important.

After stemgraphic is properly installed use the syntax below to plot a stem-and-leaf plot with dataset saved in x.

import stemgraphic
stemgraphic.stem_graphic(x, scale = 10,aggregation=False)

Note: We need to execute(run) pip install stemgraphic for every new session to graph stem-and-leaf plot.

Example: The dataset below is exam scores. Construct stem-and-leaf plot.

33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74, 78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100

Watch demo video

  1. Click + Code
  2. Type pip install stemgraphic
  3. Click play button
  4. Click + Code
  5. Type import stemgraphic
  6. Type x = [33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74,78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100]
  7. Type stemgraphic.stem_graphic(x,scale=10,aggregation=False)
  8. Click play button
import stemgraphic
x = [33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74, 
           78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100] 
stemgraphic.stem_graphic(x,scale=10,aggregation=False)
Show expected output
(<Figure size 540x198 with 1 Axes>,
 <matplotlib.axes._axes.Axes at 0x7fcbacfd48d0>)
Stem-and-leaf plot of the 31 exam scores drawn by stemgraphic with stems 3 through 10 (scale 10); the 8 stem carries leaves 0, 3, 8, 8, 8 and the 9 stem carries 0, 2, 4, 4, 4, 4, 6.

Example: The dataset below is scores of 20 students in a stat class. Construct a stem-and-leaf plot.

[81, 21, 53, 32, 80, 13, 26, 42, 35, 41, 14, 25, 87, 62, 46, 40, 32, 52, 85, 28, 17, 36, 83, 79, 18, 50, 17, 36, 36, 19]

import stemgraphic

x = [81, 21, 53, 32, 80, 13, 26, 42, 35, 41, 14, 25, 87, 62, 46, 40, 32,
       52, 85, 28, 17, 36, 83, 79, 18, 50, 17, 36, 36, 19]    

stemgraphic.stem_graphic(x,scale = 10,aggregation=False)
Show expected output
(<Figure size 540x198 with 1 Axes>,
 <matplotlib.axes._axes.Axes at 0x7f9c42819fd0>)
Stem-and-leaf plot of the 30 test scores from 13 to 87 (stems 1 through 8): the 3 stem is fullest with leaves 2, 2, 5, 6, 6, 6.

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.