Histograms and bin width · Statistics & Data Science… | StemPy Lab
Course 2.1·Lesson 1 of 3
Histograms and bin width
A histogram groups numerical data into bins of equal width and draws a touching bar for each bin. The same data can look quite different with narrow bins and with wide ones. This lesson shows how to count values into bins, how to choose a width, and how to draw bins of different widths fairly with frequency density.
about 35 min1 simulation2 practice questions1 Python program6 questions to check yourself
This lesson is free to read. Enroll free to keep your place and take the tests.
Lesson 1 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
sort numerical data into bins and draw a histogram;
explain how the bin width changes the picture, and choose a sensible width;
use relative frequency and frequency density.
Thirty journey times
A teacher asks 30 students how many minutes their journey to school takes. This is a made-up class survey. Here are the answers, in order:
4,6,7,8,9,10,11,12,12,13,14,15,15,16,17,
18,18,19,21,22, 24,25,27,29,31, 34,37,41,45,52
Can you see a pattern in the list? It is hard. Thirty numbers are too many to hold in your head. A picture helps.
Bins and bars
A histogram is a picture of numerical data. You cut the number line into equal pieces called bins (other books say classes or intervals). Then you count how many values fall in each bin. Each bin gets a bar, and the height of the bar is that count, the frequency.
Let us use bins 5 minutes wide, starting at 0.
Decide the edges. The bins are 0 to 5, 5 to 10, 10 to 15, and so on up to 50 to 55.
Decide the edge rule. A value that sits exactly on an edge goes into the bin above it. So 10 goes in the bin 10 to 15, not in 5 to 10. Then no value is counted twice.
Count. The counts are 1,4,6,7,3, 3,2,1,1,1, 1. They add up to 30, which checks that no value was lost.
Draw. The bars touch, because the number line has no gaps. A bar chart of categories has gaps between its bars. A histogram does not.
The same data told two ways
The picture below shows the same 30 times with three bin widths.
Three histograms of the same 30 journey times in minutes, side by side, all on the same scale from 0 to 60 minutes. The first uses bins 2 minutes wide and looks ragged, with many bars of height 1, 2 or 3. The second uses bins 5 minutes wide and shows a hill between 10 and 20 minutes and a long thin tail to the right. The third uses bins 10 minutes wide and shows six bars, with one tall bar from 10 to 20 minutes.
With bins 2 minutes wide, most bars hold 1, 2 or 3 students. The picture is bumpy, and the shape is hard to see.
With bins 5 minutes wide, a clear shape appears: a hill between 10 and 20 minutes and a long thin tail to the right.
With bins 10 minutes wide, the hill is still there, but the details have gone. One bar holds 13 students.
No width is the only right one. Too narrow, and you see noise. Too wide, and you hide the shape. A common starting point is a number of bins close to the square root of the number of values. For 30 values, 30≈5.5, so try 5 or 6 bins, and then try a few widths near that. Always say which bins you used.
You should find that a width of 2 minutes gives a ragged outline, and a width of 12 minutes leaves only five bars, with 13 students in one of them. Moving the start also changes the bars a little. That is a good reason to look at more than one histogram before you describe a shape.
Relative frequency
Sometimes you want to compare two groups of different sizes. Then divide each count by the number of values. The result is the relative frequency, a share of the whole. The bin 15 to 20 holds 7 of the 30 students, a relative frequency of 307≈0.233, or about 23%. A histogram of relative frequencies has exactly the same shape. Only the numbers on the vertical axis change.
Bins of different widths
What if the bins are not equal? Suppose the times are grouped as 0 to 10, 10 to 20, 20 to 30 and 30 to 60 minutes. The counts are 5, 13, 6 and 6. The last bin is three times as wide as the others. If you draw its height as 6, its bar has three times the area of the 20 to 30 bar, which also holds 6 students. The eye reads area, so the picture lies.
The fix is to make the area of each bar stand for its count. The height is then the frequency density:
frequency density=bin widthfrequency
For the bin 30 to 60 minutes, the frequency density is 30min6=0.2 students per minute. For the bin 10 to 20 minutes, it is 10min13=1.3 students per minute.
Two histograms of the journey times grouped as 0 to 10, 10 to 20, 20 to 30 and 30 to 60 minutes. On the left, marked wrong, the heights are the frequencies 5, 13, 6 and 6, so the wide last bar looks very large. On the right, marked right, the heights are frequency densities of 0.5, 1.3, 0.6 and 0.2 students per minute, so the wide last bar is low and its area matches its count.
Many school books use equal bins only. A-Level and some university courses use frequency density whenever the widths differ.
Practise with new numbers
Try it in Python
The program counts the journey times into bins with a loop, prints each count, and then draws the histogram with matplotlib.
Common mistakes
Gaps between the bars. That is a bar chart of categories. Histogram bars touch.
Counting an edge value twice, or not at all. Choose one rule and keep it: an edge value goes into the bin above.
Unequal bins drawn by frequency. A wide bin then looks far too important. Use frequency density.
Trusting one histogram. Try two or three widths before you describe the shape.
Remember these
A histogram shows numerical data in bins of equal width, with touching bars.
The height of a bar is the frequency, or the relative frequency when you divide by the total.
Narrow bins show noise; wide bins hide detail. About n bins is a starting point.
With unequal bins, height = frequency density = frequency divided by width.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Bins are 20 to 25, 25 to 30 and 30 to 35 seconds. Into which bin does a time of exactly 25 seconds go?
A histogram of 60 values has a bar of height 15. What is its relative frequency?
Data run from 3 kg to 38 kg. How many bins 5 kg wide, starting at 0, are needed?
A bin from 40 to 70 minutes holds 12 values. What is its frequency density?
Why can a histogram with very wide bins hide important features?
References
OpenStax, Introductory Statistics, Chapter 2 (Descriptive Statistics), section on histograms, frequency polygons and time series graphs.
Enroll free in Histograms and the Shape of Data
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.