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

Free lesson

Choosing a chart that answers the question

After this lesson: I can choose a chart that answers a question.

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

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

WordWhat it meansExample
Bar chartone bar for each group, its length the value
Line chartpoints joined in order, usually over time
Histogrambars that count how many values fall in each range
Binone range of values in a histogram250 to 299
Scatter plotone dot for each row, placed by two numbers
Axisthe 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 hiresChartpandas
Which weather is busiest?Bar chartmeans.plot.bar()
How do hires change through the year?Line chartby_month.plot.line()
How many days were quiet, normal or busy?Histogramhires["hires"].plot.hist()
Are warm days busier?Scatter plothires.plot.scatter(x="temp_c", y="hires")

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

Mean hires for 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.

Type this 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"]
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

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.

Bins by hand

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

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

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

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.