Charts with matplotlib · lesson 6 of 11
Parts in this lesson
Aax.spines[["top", "right"]].set_visible(False)hide the box round the chart
Bax.bar_label(bars, fmt="{:,.0f} kg")a number on each bar
Cax.annotate(text, xy=..., xytext=..., arrowprops=...)a note with an arrow
DStrMethodFormatter("₱{x:,.0f}")pesos with commas on an axis

Making charts clear

Last updated

A chart straight from the defaults shows the data. A clear chart tells the reader what to see in it, in the two seconds they'll give it. In this lesson you'll strip a cluttered chart down to its message, write titles that state the finding, label bars directly, point at what matters with an annotation, and format pesos so no one has to count zeros.

By the end of this lesson you can

  • Remove clutter: spines, heavy grids, legends with one entry
  • Write a title that states the finding, and label values directly
  • Add an annotation with an arrow
  • Format axis numbers with units, such as ₱ and thousands separators

Declutter

Edward Tufte called the ink in a chart that shows data data-ink, and argued that most of the rest can go. You don't have to go as far as he did, but the idea is a good test: for each mark on the chart, ask what it tells the reader. Step through one chart losing a piece at a time.

A chart losing its clutter. A chart straight from the defaults, plus a few common extras. It answers "which store sold the most rice?", slowly.BatangasLipaRosarioTanauan01,0002,0003,000Chart 1valuekilos
1 of 7 A chart straight from the defaults, plus a few common extras. It answers "which store sold the most rice?", slowly.

Nothing was added except the labels and one colour. The chart got clearer by removing things: the background, the heavy grid, two spines, a pointless legend, and the y-axis once the bars had their own numbers. And the title changed from naming the chart to saying what it shows.

A title that states the finding is the biggest single change. "Rice sold by store" makes the reader work it out; "Batangas sold twice as much rice as Rosario" tells them, and they can check it against the bars.

The clear version, in code

Run the cells: the default chart, then the clear one.

clear.ipynb

The third cell, line by line:

  • sort_values(ascending=False) puts the biggest first.
  • colours is a list with one colour per bar: pink for Batangas, grey for the rest. color=colours hands it to ax.bar. Lesson 6.7 is about choosing colours.
  • bars = ax.bar(...) keeps what ax.bar returns, the bars, so that ax.bar_label(bars, fmt="{:,.0f} kg", padding=3) can write each bar's value on top of it. The format is the same mini-language as an f-string (:, adds commas, .0f means no decimals), and padding leaves 3 points of space.
  • ax.spines[["top", "right", "left"]].set_visible(False) hides three of the four lines round the chart. ax.set_yticks([]) removes the y ticks: the bar labels say the numbers more exactly than ticks could.
  • loc="left" lines the title up with the left edge, where the eye starts reading.

Change the highlighted store to Rosario, and rewrite the title to say something true about Rosario.

Pointing at what matters

On a busy line, the reader might not find the point you mean. An annotation puts a note on the chart with an arrow to it. Run the cells.

annotate.ipynb
  • daily.idxmax() finds the date of the biggest value (3.5): Saturday 20 January, 239.4 kg across all stores.
  • ax.annotate(text, xy=..., xytext=..., arrowprops=...): xy is the point the arrow touches, in the data's own units (a date and kilos); xytext is where the words go; arrowprops={"arrowstyle": "->"} draws the arrow between them. pd.Timedelta(days=4) moves the words four days to the right of the point.
  • The line is grey, so the black note stands out against it.

Use annotations sparingly: one or two per chart, for the thing the title talks about. A label on every point is clutter again.

Wrong way: an axis called “value”

The total pesos each store took in January and February: kilos times each item's price from products.csv. Run the first two cells.

units.ipynb

The second chart's y-axis says value, and its ticks run from 0 to 175000 with no commas. (Past a million, matplotlib switches to writing 1e6 in the corner, which is worse.) Value of what? Kilos? Pesos? Thousands of pesos? Per day, or in total? The reader can't tell, and someone will guess wrong. "Value", "amount", "count" and a column's raw name (pesos) are all labels that make the reader do your job.

The third cell fixes it:

  • StrMethodFormatter("₱{x:,.0f}"), from matplotlib.ticker, formats each tick as pesos with commas: ₱50,000. Inside the braces, x is the tick's value and the rest is f-string formatting.
  • set_ylabel("sales, January and February") says what and when. The units are now on the ticks themselves.

The check: cover the title and ask whether the axes alone say what was measured and in what units. If not, fix the labels.

Check what you learned

Which title is best for a chart of monthly sales that fell after March?
Predict: what does "{:,.0f} kg".format(2744.3) give, the format bar_label used?
A chart has one line, and a legend that says “sales”. What should you do?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis