Three times as tall
An advert shows two bars. Our phone's battery bar is three times as tall as the rival's. It looks like a huge win.
Now read the numbers: 21 hours against 19. Nobody lied about the numbers. The chart lied about the gap. Today you learn to spot that, and to draw charts that do not.
By the end of this lesson
Note
I can choose an honest chart.
Key words
| Word | What it means | Example |
|---|---|---|
| Axis | the line along the side of a chart that carries the scale | |
| Bar chart | one bar for each group, its length the value | |
| Line chart | points joined in order, often over time | |
| Scatter chart | one dot for each record, two numbers for its place | |
| Histogram | bars that count how many values fall in each range | |
| Bin | one range of values in a histogram | 250 to 299 |
The cells students fill in are left blank in this preview.
Before you start
Note
Press Start the lab on the bar at the top and wait until it is running. Some scripts save a chart as a picture. Open JupyterLab from the bar and double-click the picture in the file list to see it.
Each chart answers one kind of question
Start from the question, not the chart. A bar chart compares groups. A line chart shows a change over time, so the points must come in order.
A scatter chart shows whether two numbers move together. A histogram shows how values spread out: how many days were quiet and how many were busy.
Worked example: questions about the shop
| Question | Chart | Why |
|---|---|---|
| Which weather is busiest? | Bar chart | three groups to compare |
| How do hires change through the year? | Line chart | months come in order |
| Do warmer days get more hires? | Scatter chart | two numbers for each day |
| How many days were very busy? | Histogram | how hires spread out |
The cells students fill in are left blank in this preview.
Mean hires in 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.
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"]
# Put the months in calendar order, or the line jumps about.
months = hires.groupby("month")["hires"].mean().reindex(order)
months.plot.line(marker="o", title="Mean hires a day in each month")
A histogram counts values into bins
A histogram cuts the numbers into bins of equal width, such as 0 to 49, 50 to 99 and so on. Each value lands in exactly one bin.
The height of each bar is how many values landed in its bin. Too few bins hide the shape. Too many show only noise.
Count into bins by hand
# Hires on ten days, put into bins 100 wide.
days = [41, 87, 120, 270, 273, 299, 300, 345, 410, 487]
width = 100
counts = [0, 0, 0, 0, 0]
for one in days:
place = one // width
counts[place] = counts[place] + 1
print(counts)
# Which bin has the most days?
# Now change 345 to 245 and press Run. What changes?
Count the year into bins of 50
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.
A bar must start at zero
You judge a bar by its length. So a bar chart's axis must start at zero, or the lengths lie.
axis.py draws the mean hires for each weather twice. The left chart starts at 0. The right starts at 220, just below the rainy mean. It prints how many times taller the sunny bar is than the rainy bar in each.
Draw the same bars twice
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 a bar chart may start its axis wherever the data starts. That is true for a line chart, which shows change. A bar's length is the value, so the bar must start at zero. In the lab, starting at 220 made a gap of 1.37 times look like 12.
Going further: fix the advert
In your notebook, draw the advert's two bars: 21 hours and 19 hours. Use pd.Series([21, 19], index=["Ours", "Rival"]).plot.bar().
Then add two lines under it, import matplotlib.pyplot as plt and plt.ylim(18, 22), and run the cell again. Write one sentence on which chart a fair advert should use, and why.