Stem and Leaf Plots in Python (original) (raw)
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
- Separate each data value into a stem (all digits except the last) and a leaf (last digit).
- Ensure the leaf has only one digit, while the stem can have multiple digits.
- Write stems in a vertical column in ascending order.
- Draw a vertical line next to the stems.
- 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])))}")
`
**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])))}")
`