What is a histogram and how do you read it?
See how one Saturday's tickets are spread with a histogram, and discover that two branches with the same average have very different customers.
Free course. Your Maestro, an AI, .
On the first page you saw two equal bars: the average Saturday ticket was 95 pesos at both Centro and Zapopan. And maybe you thought their customers are alike. This page gives you the chart that actually settles that doubt: the histogram.
The question it answers is about distribution: "how are the values spread?" It doesn't ask for one number that sums everything up, but for the whole shape: are almost all the tickets close to the same amount, or is it all over the place? Is there one group or two? Are there a few very large ones pulling the average?
How it's built
A histogram takes a Saturday's 120 tickets and sorts them into ranges of equal width: from 40 to 50 pesos, from 50 to 60, from 60 to 70, and so on. Then it draws one bar per range, as tall as the number of tickets that fell into it. It looks like a bar chart, with two differences that matter when you read it:
- The bottom axis is a number (pesos), not a list of names. That's why the bars touch, with no gap between them: one range ends where the next one begins.
- The bars are never sorted by height. They go in the order of the numbers, because the shape is exactly what you want to see.
Here's how Centro's tickets are spread.
Step 1 of 4
The whole chart
Along the bottom, the ticket amount in 10-peso ranges. On the left, how many tickets fell in each range. These are the 120 tickets of one Saturday at Centro (synthetic data, CC0).
How to read a histogram
When you see one, ask yourself four questions, in this order:
- Where's the hump? It's the value most of the data gathers around.
- How wide is it? A narrow hump says almost everyone is alike; a wide one, that there's a bit of everything.
- How many humps are there? Two humps are almost always two different groups mixed together: two kinds of customer, two shifts, two machines.
- Is there a long tail? A few very large (or very small) values on one side pull the average toward them.
One detail changes what you see: the width of the ranges. With very wide ranges, two groups can fall into the same bar and disappear. With very narrow ranges, each bar holds one or two tickets and the shape turns into noise. There's no correct width: try a couple and keep the one that shows the shape without inventing peaks.
If you have something similar at work, like delivery times or invoice amounts, ask your Maestro what shape to expect.
Try it
This code draws the Centro histogram you just walked through. Now it's time to see Zapopan's, the branch with the same average.
Make two changes (the columns are in Spanish: montomxn is the ticket amount in pesos, sucursal the branch): 1. On the last line, change 'Centro' to 'Zapopan' to see the other branch. 2. Change bin=True to bin=alt.Bin(step=20), so each range is 20 pesos wide. You're done when you
Your turn
Make two changes (the columns are in Spanish: monto_mxn is the ticket amount in pesos, sucursal the branch):
- On the last line, change
'Centro'to'Zapopan'to see the other branch. - Change
bin=Truetobin=alt.Bin(step=20), so each range is 20 pesos wide.
You're done when you see Zapopan's histogram. Before you read on, look at the shape: does it look like Centro's? Where would the 95-peso average fall? If you get an error, check the single quotes around Zapopan, or paste the error to your Maestro.
The chart redraws when you pause typing, or right away with Run. Ctrl+Enter runs it. Tab indents; to leave the editor, press Escape, then Tab.
Ready to run your code.
Data: tickets-sabado, 240 rows
Enter saves a cell and Escape leaves it as it was. Leave a cell empty for a missing value.
| Row | Remove | ||
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 |
Zapopan doesn't have a hump: it has two groups separated by a huge gap. On the left, 108 tickets under 80 pesos: people stopping for a coffee and a concha on the way to work. On the right, 12 tickets of 400 pesos or more: birthday cakes. Between 80 and 400 pesos there's not a single ticket.
Now go back to the question from the first page. Zapopan's average is 95 pesos, the same as Centro's, but it lands right in the gap: it's an amount no customer paid that Saturday. At Centro the average describes the typical customer; at Zapopan it describes nobody. If someone plans Zapopan's display case around "the 95-peso customer", they'll stock it for a person who doesn't exist.
That's the power of a histogram: it shows what a single number hides. In module 4 you'll see this same trap from the other side, when someone shows you only the average.
Still not clear why the average lands in the gap? Ask your Maestro to explain it with small numbers.