Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Charts that tell the truth

After this lesson: I can choose an honest chart.

45 min lesson3 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

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

WordWhat it meansExample
Axisthe line along the side of a chart that carries the scale
Bar chartone bar for each group, its length the value
Line chartpoints joined in order, often over time
Scatter chartone dot for each record, two numbers for its place
Histogrambars that count how many values fall in each range
Binone range of values in a histogram250 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

QuestionChartWhy
Which weather is busiest?Bar chartthree groups to compare
How do hires change through the year?Line chartmonths come in order
Do warmer days get more hires?Scatter charttwo numbers for each day
How many days were very busy?Histogramhow hires spread out

The cells students fill in are left blank in this preview.

Mean hires in each month

Command
docker exec jupyter python3 steps/months.py

Shown 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

Code: python
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

Code: python
# 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

Command
docker exec jupyter python3 steps/hist.py

Shown 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

Command
docker exec jupyter python3 steps/axis.py

Shown 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.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.