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.
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.
The third cell, line by line:
sort_values(ascending=False)puts the biggest first.coloursis a list with one colour per bar: pink for Batangas, grey for the rest.color=colourshands it toax.bar. Lesson 6.7 is about choosing colours.bars = ax.bar(...)keeps whatax.barreturns, the bars, so thatax.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,.0fmeans no decimals), andpaddingleaves 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.
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=...):xyis the point the arrow touches, in the data's own units (a date and kilos);xytextis 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.
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}"), frommatplotlib.ticker, formats each tick as pesos with commas: ₱50,000. Inside the braces,xis 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
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.
- matplotlib: Annotations: annotate, xy and xytext, and arrow styles
- matplotlib: matplotlib.axes.Axes.bar_label: labels on bars, with a format
- matplotlib: matplotlib.ticker: StrMethodFormatter and the other tick formatters
- matplotlib: matplotlib.axes.Axes.set_title: loc="left"