LambdaLab
Informatics Practices · Class 12 · Data Visualization
Data VisualizationHands-on⏱️ 10 min read

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

Imagine standing over a row of buckets, each labelled with a temperature range: 18–20°C, 20–24°C, 24–28°C. You read out your thirty daily temperatures one by one and drop each reading into the bucket it belongs to. When you're done, the height of each bucket's pile is a bar of your histogram. The buckets are the bins.

Don't just picture it — do it. Drop the readings in and watch the piles grow, then squash them flat.

The row of buckets

Drop each of the 30 daily temperatures into the bucket whose range it belongs to. The pile that builds up is the histogram bar.

Next reading:22Mild
18 – 20
🧊 Cold · 0
20 – 24
🌤️ Mild · 0
24 – 28
🔥 Warm · 0

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.

📊 Bar Chart
  • Compares categories — Shivaji vs Tagore vs Ashoka.
  • X-axis holds names (discrete groups).
  • Bars have gaps — the categories are unrelated.
  • Function: plt.bar()
🗃️ Histogram
  • 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()
The give-away is the gap
In a bar chart the bars have gaps, because Shivaji and Tagore are separate, unrelated categories. In a histogram the bars touch, because 20–22°C runs continuously into 22–24°C. The touching bars are the message.

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.

histogram_auto.py

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.

The bin explorer

Thirty daily temperatures, sorted into buckets. Change how the buckets are made and watch the shape of the data change with them.

0246182022242628Distribution of Daily TemperaturesTemperature (°C)Number of Days
[18, 19)1[19, 20)2[20, 21)4[21, 22)3[22, 23)4[23, 24)4[24, 25)4[25, 26)4[26, 27)2[27, 28]2
The 30 readings, coloured by bin
222325242622212019202324252728262524232221201819202122232425
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.

histogram_bins.py
Changing the bins changes the story
The same thirty readings can look bell-shaped, flat, or jagged depending purely on how many bins you choose. That is not a flaw — it is why you should try a few and pick the one that shows the structure honestly, rather than accepting the first chart you get.

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.

histogram_custom.py

A list of 4 edges makes 3 bins. Here, custom_temp_bins tells pyplot to create these ranges:

BinRangeIncludesCount
118 → 2018 up to but not including 203 days
220 → 2420 up to but not including 2415 days
324 → 2824 up to and including 2812 days

5Recap

You writepyplot doesUse when
plt.hist(data)Picks the bins itselfA first, quick look
plt.hist(data, bins=6)6 equal bins, min → maxYou want a specific level of detail
plt.hist(data, bins=[18,20,24,28])Bins at exactly those edgesYou have meaningful categories
Key takeaway
Bins are the intervals that sort your continuous data, and pyplot gives you three levels of control over them: let it decide, give it a count, or give it the exact edges. Everything else about a histogram follows from that one choice.
Quick Check

What does the height of a histogram bar represent?

Quick Check

plt.hist(data, bins=[0, 10, 20, 30]) creates how many bins?

Quick Check

In plt.hist(data, bins=[18, 20, 24, 28]), where does a reading of exactly 20 go?

Quick Check

Your data is the marks of 500 students and you want to see how they are spread. Which chart?

Chapter complete!
You can now turn numbers into line plots, multi-line comparisons, bar charts and histograms — and, just as importantly, choose which of them a given question deserves. 🎉