Basic

A chart that answers the question

A chart is an answer, not a decoration. Pick the comparison the reader needs to make, then choose the mark that makes that comparison easy. Bars compare lengths, lines show change over an ordered axis, and a pie forces the hardest comparison of all.

What you will see

0 - a bar of zero length. Round up to at least 1 so a non-zero value is always visible.

About 1 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Print a proportional text bar chart.

1

Scale to a fixed width

summary = [('north', 412), ('south', 89), ('east', 4)] mx = max(v for _, v in summary) for k, v in summary: print(f"{k:<8}{'#' * round(30 * v / mx):>30} {v}")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Three lines with bars proportional to the values, longest 30 characters.
2

Notice the tiny bar

print(round(30 * 4 / 412))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
0 - a bar of zero length. Round up to at least 1 so a non-zero value is always visible.
Rounding to zero is how a real value disappears from a chart.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Numbers become marks a reader can compare.
1 Decide the comparison the reader needs
↓
2 Choose the mark that makes it easy
↓
3 Sort and scale the values
↓
4 Label the axis and the claim
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

summary = [('north', 412), ('south', 89), ('east', 4)] sets nothing on screen
mx = max(v for _, v in summary) sets nothing on screen
for k, v in summary: sets nothing on screen
print(f"{k:<8}{'#' * round(30 * v / mx):>30} {v}") Three lines with bars proportional to the values, longest 30 characters.
Why this works
Start the axis at zero

A bar chart that starts at the minimum value makes a 5 percent difference look like a 90 percent difference. Bars must start at zero; lines may not, but then say so in the title.

One chart, one claim

Write the sentence the chart is meant to support above it. If you cannot write that sentence, the chart is exploratory and should not be in the report.

Examples

Bar for comparing categories

Bars share a baseline, so lengths compare reliably. Sort them by size. This is the default choice for 'which is biggest' questions and it works in black and white.

Line for change over time

Time goes on the x axis in order, so a line shows trend and seasonality that a table hides. Use one line per series and no more than a handful, or the chart becomes a tangle.

A text bar chart is enough

Printing 'region ......... 412' with proportional dots is a legitimate chart for a terminal or a report. Reaching for a plotting library before you have checked the numbers is where charts go wrong.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Grouping and aggregating
Previous Lesson
Grouping and aggregating
Next Lesson
Missing values
Missing values →