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

Meet the distribution.

Explore how numeric observations are spread rather than comparing categories.

ADVANCED LAB
BY NAVAN SAMI◷ 10 MIN READPYTHON 3

01 The idea

A histogram groups numeric observations into intervals and counts the observations in each interval. Bin boundaries change the visible shape, so they are an analysis choice rather than a decorative setting. Inspect the sample size and units, try reasonable alternative bins, and avoid claiming that a tiny sample reveals a universal distribution.

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_histograms.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
durations = [8, 12, 13, 18, 22, 24, 25, 32, 35, 41, 44, 47]
fig, ax = plt.subplots(figsize=(6, 3.5), layout="constrained")
ax.hist(durations, bins=[0, 10, 20, 30, 40, 50], color="#ed5027", edgecolor="white")
ax.set(xlabel="Task duration (minutes)", ylabel="Number of tasks", title="How long do tasks take?")
ax.set_xticks([0, 10, 20, 30, 40, 50])
fig.savefig("matplotlib-histograms.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved matplotlib-histograms.png")
↳ EXPECTED OUTPUT
Saved matplotlib-histograms.png
Histograms, bins & distributions: chart generated by the Python example
THE OUTPUT, MADE VISIBLE · Generated from this lesson’s example.
Download chart ↗

03 Under the hood

  1. 1

    The numeric bin boundaries make the intervals explicit. Most bins include the left edge and exclude the right; the final bin includes its right edge.

  2. 2

    The y-axis counts observations here. density=True changes the interpretation to probability density, not directly to a percentage per bin.

  3. 3

    Consider outliers and missing data before plotting. Show both the summary and what was excluded when an exclusion materially changes the story.

04 Your turn, builder

A SMALL MISSION

Create the same histogram with wider intervals and compare what information becomes less visible.

Reveal one possible solution +
one_possible_solution.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
values = [8, 12, 13, 18, 22, 24, 25, 32, 35, 41, 44, 47]
fig, ax = plt.subplots(figsize=(6, 3.5), layout="constrained")
ax.hist(values, bins=[0, 25, 50], edgecolor="white", color="#ed5027")
ax.set(xlabel="Minutes", ylabel="Task count", title="A coarser view")
fig.savefig("challenge.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved challenge.png")

05 Check your signal

QUICK SIGNAL CHECK

What determines which interval contains a value?

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.