Histograms & Bins
Histograms help us understand the distribution of a dataset by showing how many data points fall into different ranges. Think of it as sorting your data into slots, then counting how many items landed in each one. Those slots are called bins.
The Row of Buckets
Don't just picture it — do it. Drop the readings in and watch the piles grow, then squash them flat.
Drop each of the 30 daily temperatures into the bucket whose range it belongs to. The pile that builds up is the histogram bar.
Each reading falls into exactly one bucket. A bucket owns its lower edge but not its upper one — so a reading of exactly 20 lands in Mild, not Cold.
1First: how is this different from a bar chart?
This is the single most common confusion in the chapter, and it is worth settling before we touch any code. They look almost the same. They answer completely different questions.
- Compares categories — Shivaji vs Tagore vs Ashoka.
- X-axis holds names (discrete groups).
- Bars have gaps — the categories are unrelated.
- Function:
plt.bar()
- Shows a distribution — how often each range occurs.
- X-axis holds numbers split into bins.
- Bars touch — the ranges run continuously into each other.
- Function:
plt.hist()
2Example 1: the “quick glance” histogram
Imagine you tracked the daily high temperature in your city for a month and want a quick visual of which ranges were most common. Call plt.hist() with just your data and pyplot does the thinking for you — it works out a suitable number of bins and their intervals, counts how many readings fall in each, and draws a bar for each count.
3Example 2: setting the number of bins
Sometimes you want control over the level of detail. Pass bins as an integer and pyplot creates that many equally spaced bins, from the smallest value in your data to the largest.
Drag the slider below. Notice that with only 2 bins the shape disappears, and with 12 the chart becomes spiky noise — the number of bins is a real analytical choice, not a cosmetic one.
Thirty daily temperatures, sorted into buckets. Change how the buckets are made and watch the shape of the data change with them.
import matplotlib.pyplot as plt
daily_temperatures = [
22, 23, 25, 24, 26, 22, 21, 20, 19, 20,
23, 24, 25, 27, 28, 26, 25, 24, 23, 22,
21, 20, 18, 19, 20, 21, 22, 23, 24, 25
]
# pyplot picks the bins for you
plt.hist(daily_temperatures)
plt.title("Distribution of Daily Temperatures")
plt.xlabel("Temperature (°C)")
plt.ylabel("Number of Days")
plt.show()Called with just the data, pyplot works out a sensible set of bins for you — here, ten equal ranges.
4Example 3: defining custom bin ranges
What if you have specific ranges you care about? Perhaps you want to categorize temperatures as Cold (below 20°C), Mild (20–24°C) and Warm (above 24°C). Pass bins a list of edges and you define the exact start and end of every bin — and they need not be equal in width.
A list of 4 edges makes 3 bins. Here, custom_temp_bins tells pyplot to create these ranges:
| Bin | Range | Includes | Count |
|---|---|---|---|
| 1 | 18 → 20 | 18 up to but not including 20 | 3 days |
| 2 | 20 → 24 | 20 up to but not including 24 | 15 days |
| 3 | 24 → 28 | 24 up to and including 28 | 12 days |
5Recap
| You write | pyplot does | Use when |
|---|---|---|
| plt.hist(data) | Picks the bins itself | A first, quick look |
| plt.hist(data, bins=6) | 6 equal bins, min → max | You want a specific level of detail |
| plt.hist(data, bins=[18,20,24,28]) | Bins at exactly those edges | You have meaningful categories |
What does the height of a histogram bar represent?
plt.hist(data, bins=[0, 10, 20, 30]) creates how many bins?
In plt.hist(data, bins=[18, 20, 24, 28]), where does a reading of exactly 20 go?
Your data is the marks of 500 students and you want to see how they are spread. Which chart?