runtime
220 / 250
lesson 220 Visualization & EDA · advanced

Charts that make a point

A chart for a report should make its point without a caption: sort the bars, highlight the one that matters, say the finding in the title, and label the values readers need.

You'll be able to
  • Sort categories so the order itself carries information
  • Highlight one bar with its own colour and keep the rest neutral
  • Build the title from the data (an f-string), add value labels with bar_label and a target line with axhline
The idea

An exploratory chart helps *you* find something; an explanatory chart tells *someone else* what you found. Four habits do most of the work. Sort bars so the order means something. Highlight the bar the story is about in a strong colour and draw the others in grey. Say the finding in the title ("Web brings in two thirds of revenue"), computed from the data so it is never stale. Label the values readers will quote, with ax.bar_label(bars), and draw targets or limits with ax.axhline(value).

Run each example to see what it prints. Running examples doesn't count toward progress.

example-1.py · Which channel brings in the most?
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import pandas as pd

revenue = pd.Series({"web": 429.63, "app": 128.56, "phone": 54.55, "marketplace": 17.75}).sort_values(ascending=False)
share = revenue.iloc[0] / revenue.sum()
colours = ["tab:blue"] + ["lightgray"] * (len(revenue) - 1)
fig, ax = plt.subplots()
bars = ax.bar(revenue.index, revenue.values, color=colours)
ax.bar_label(bars, fmt="%.0f")
ax.set_title(f"{revenue.index[0]} brings in {share:.0%} of revenue")
ax.set_ylabel("Revenue (USD)")
print(ax.get_title())
print([mcolors.to_hex(p.get_facecolor()) for p in ax.patches])
print([t.get_text() for t in ax.texts])
plt.show()
example-2.py · A colour matplotlib doesn't know
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.bar(["web", "app"], [429.63, 128.56], color=["tab:blue", "grey50"])
example-3.py · Delays against a 15-minute target
import matplotlib.pyplot as plt
import pandas as pd

delays = pd.Series({"AA": 39.75, "DL": 14.5, "MQ": 27.0, "OO": -22.0, "UA": -4.0}).sort_values(ascending=False)
fig, ax = plt.subplots()
ax.bar(delays.index, delays.values, color=["tab:red" if d > 15 else "lightgray" for d in delays])
target = ax.axhline(15, linestyle="--", color="black")
ax.annotate("on-time limit", xy=(4, 15), xytext=(3.2, 25))
ax.set_title(f"{(delays > 15).sum()} of {len(delays)} airlines averaged more than 15 minutes late")
print(ax.get_title())
print(delays.index.tolist(), list(target.get_ydata()))
plt.show()

Mistakes to avoid: a title that only names the axes ("Revenue by channel") when the reader needs the finding; a rainbow of colours that highlights nothing; truncated y-axes on bar charts (bars must start at 0, or their lengths lie); and a typed-in number in the title that goes stale when the data changes.

Review
  • Sort bars so the order carries meaning; highlight one colour, keep the rest neutral.
  • State the finding in the title, computed with an f-string from the same data.
  • bar_label adds values, axhline draws a target, annotate labels a point.
  • Next (see Data Formats & Analytical Workflows): store and move the data behind these charts: Parquet, chunks, SQL and exports.
Practise this

Loading your progress…

To master this lesson, solve both challenges without hints or the answer, and pass the concept check.

Charts that make a point — Visualization & EDA · Runtime