A small gap that looked huge
A manager sees a bar chart in a report. The sunny bar towers over the rainy one, so she plans twelve times as many bikes for sunny days.
The real gap is far smaller. The chart was not wrong in its numbers. It was wrong in its axis, and it answered the wrong question.
By the end of this lesson
Note
I can choose a chart that answers a question.
Key words
| Word | What it means | Example |
|---|---|---|
| Bar chart | one bar for each group, its length the value | |
| Line chart | points joined in order, usually over time | |
| Histogram | bars that count how many values fall in each range | |
| Bin | one range of values in a histogram | 250 to 299 |
| Scatter plot | one dot for each row, placed by two numbers | |
| Axis | the scale along one edge of a chart |
The cells students fill in are left blank in this preview.
The question picks the chart
Start from the question, not the chart. To compare groups, use a bar chart. To show change over time, use a line chart.
To see how one number is spread, use a histogram. To ask whether two numbers move together, use a scatter plot.
A pie chart is rarely the best choice. People judge lengths far better than angles.
Worked example: four questions, four charts
| Question about the hires | Chart | pandas |
|---|---|---|
| Which weather is busiest? | Bar chart | means.plot.bar() |
| How do hires change through the year? | Line chart | by_month.plot.line() |
| How many days were quiet, normal or busy? | Histogram | hires["hires"].plot.hist() |
| Are warm days busier? | Scatter plot | hires.plot.scatter(x="temp_c", y="hires") |
The cells students fill in are left blank in this preview.
Mean hires for each month
docker exec jupyter python3 steps/months.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Type this in your notebook: a line chart
import pandas as pd
hires = pd.read_csv("hires.csv")
order = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
by_month = hires.groupby("month")["hires"].mean().reindex(order)
# A line, because the months come in order.
by_month.plot.line(marker="o", title="Mean hires a day")
A histogram counts values into bins
A histogram cuts a number line into bins of equal width. Each value lands in exactly one bin. The bar's height is how many values landed there.
With bins 50 wide, a day of 273 hires goes in the bin from 250 to 299. Divide by the width and drop the remainder: 273 // 50 is 5, the sixth bin counting from 0.
Count the days into bins
docker exec jupyter python3 steps/hist.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Bins by hand
# Put each day's hires into a bin 100 wide.
days = [41, 87, 120, 270, 273, 299, 300, 487]
width = 100
counts = [0, 0, 0, 0, 0]
for one in days:
place = one // width
counts[place] = counts[place] + 1
print(counts)
# Change width to 50 and counts to ten zeros.
# Which bin is now the fullest?
Type this in your notebook: change the bins
# Few wide bins hide the shape; many thin ones show noise.
hires["hires"].plot.hist(bins=4)
A bar must start at zero
A reader judges a bar by its length. So a bar chart's value axis must start at zero, or the lengths lie.
axis.py draws the mean hires by weather twice. The left chart starts at 0. The right starts at 220, just below the rainy mean. It saves both as axis.png. Open it from the file list in JupyterLab.
Draw one chart two ways
docker exec jupyter python3 steps/axis.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think an axis can start anywhere on any chart. On a bar chart the length is the value, so start at zero. A line chart shows change rather than size, so its axis may start near the data, as long as the labels say so.
Going further
In your notebook, draw one histogram of hires for sunny days and one for rainy days, on the same bins. Use range(0, 550, 50) as the bins and alpha=0.5 so both show.
Write one sentence a manager could act on. Then say which chart from this lesson would answer the same question in a single number for each group.