Stem and Leaf Plots in Python

Last Updated : 30 Jan, 2026

A Stem-and-Leaf Plot is a way of representing quantitative data that shows the frequency distribution of values, similar to a histogram. It is particularly useful for smaller datasets and presents data in a textual, tabular format. Each value is split into a stem (all but the last digit) and a leaf (the last digit).

Example of Splitting Values:

"17" is split into "1" (stem) and "7" (leaf)
"69" is split into "6" (stem) and "9" (leaf)

Procedure to Create a Stem-and-Leaf Plot

  1. Separate each data value into a stem (all digits except the last) and a leaf (last digit).
  2. Ensure the leaf has only one digit, while the stem can have multiple digits.
  3. Write stems in a vertical column in ascending order.
  4. Draw a vertical line next to the stems.
  5. Place the corresponding leaves in ascending order on the right side of each stem.

Example: Suppose 10 writers submitted 100 articles each, and the number of articles with errors per writer is:

16, 25, 47, 56, 23, 45, 19, 55, 44, 27

Stem-and-leaf plot will be:

1 | 69
2 | 357
4 | 457
5 | 56

Basic Stem-and-Leaf Plot

This example demonstrates the most common case, where values are two-digit numbers and the stem represents the tens place.

Python
data = [16, 25, 47, 56, 23, 45, 19, 55, 44, 27]

stems = {}

for n in data:
    s, l = divmod(n, 10)
    stems.setdefault(s, []).append(l)

for s in sorted(stems):
    print(f"{s} | {''.join(map(str, sorted(stems[s])))}")

Output
1 | 69
2 | 357
4 | 457
5 | 56

Explanation:

  • divmod(number, 10) splits each value into stem (tens) and leaf (units).
  • setdefault() groups leaves under their corresponding stems.
  • Leaves are sorted to maintain ascending order.

Dataset with Repeated Values

Here's how stem-and-leaf plots handle duplicate data points.

Python
data = [32, 35, 32, 38, 41, 45, 41, 47, 49]

stems = {}

for n in data:
    s, l = divmod(n, 10)
    stems.setdefault(s, []).append(l)

for s in sorted(stems):
    print(f"{s} | {''.join(map(str, sorted(stems[s])))}")

Output
3 | 2258
4 | 11579

Explanation:

  • Duplicate values produce repeated leaves.
  • This helps visually identify frequency and clustering in the dataset.

Larger Numbers (Multi-Digit Stems)

This example demonstrates stem-and-leaf plots when values contain more than two digits.

Python
data = [112, 118, 125, 131, 145, 152, 159]

stems = {}

for n in data:
    s, l = divmod(n, 10)
    stems.setdefault(s, []).append(l)

for s in sorted(stems):
    print(f"{s} | {''.join(map(str, sorted(stems[s])))}")

Output
11 | 28
12 | 5
13 | 1
14 | 5
15 | 29

Explanation:

  • The stem can contain multiple digits.
  • Only the last digit is treated as the leaf.
  • This allows stem-and-leaf plots to scale to larger values while preserving structure.
Comment