Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ Make the data visible
EXPERIMENT 51 / 101

Compare without the clutter.

Compare category totals and choose a truthful baseline.

ADVANCED LAB
BY NAVAN SAMI◷ 11 MIN READPYTHON 3

01 The idea

A bar chart encodes magnitude through length, so its baseline matters. Categories can be sorted by a meaningful order or by value, depending on the question. Use horizontal bars when names are long. Keep labels explicit, start the value axis at zero for ordinary magnitude comparisons, and avoid decorating the chart with unnecessary three-dimensional effects.

02 The experiment

Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.

matplotlib_bars.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
names = ["Build", "Test", "Review"]
minutes = [40, 25, 15]
fig, ax = plt.subplots(figsize=(6, 3.5), layout="constrained")
bars = ax.barh(names, minutes, color=["#ed5027", "#a0b888", "#6c805a"])
ax.bar_label(bars, padding=3)
ax.set(xlabel="Minutes", title="Where the work goes", xlim=(0, 48))
ax.invert_yaxis()
fig.savefig("matplotlib-bars.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved matplotlib-bars.png")
↳ EXPECTED OUTPUT
Saved matplotlib-bars.png
Bar charts & categorical comparisons: chart generated by the Python example
THE OUTPUT, MADE VISIBLE · Generated from this lesson’s example.
Download chart ↗

03 Under the hood

  1. 1

    barh places category labels on the vertical axis and values along the horizontal axis.

  2. 2

    bar_label adds values to the bars. Leave enough axis space so labels do not collide with the frame.

  3. 3

    A zero baseline preserves the length comparison. If you intentionally use a different visual design, disclose the scale clearly rather than letting viewers assume ordinary bar semantics.

04 Your turn, builder

A SMALL MISSION

Create a vertical bar chart for three project budgets and label the currency on the value axis.

Reveal one possible solution +
one_possible_solution.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 3.5), layout="constrained")
bars = ax.bar(["Orbit", "Atlas", "Nova"], [120, 90, 150], color="#ed5027")
ax.bar_label(bars)
ax.set(ylabel="Budget (USD)", title="Practice budgets", ylim=(0, 175))
fig.savefig("challenge.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved challenge.png")

05 Check your signal

QUICK SIGNAL CHECK

Why normally start a bar value axis at zero?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

Experiment. Logged.

When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.

Keep exploring

Go deeper with the official Python guide ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.