← Data analysis with Python

Histograms for spread

See how values are distributed: where they bunch up and how far they stretch.

Start this lesson

A histogram shows the shape of ONE numeric column. It splits the range of values into bins (intervals) and draws a bar for how many values fall into each bin. It answers questions like "are most delivery times short, with a few very long ones?" or "are test scores bunched in the middle?". ax.hist(values, bins=6) draws one with 6 bins. A long tail on one side is called skew; it is a warning that the mean may be pulled away from the median.

import matplotlib.pyplot as plt

delivery_minutes = [22, 25, 19, 31, 27, 24, 45, 23, 28, 26, 60, 21, 29, 33, 24, 52, 27, 25]
fig, ax = plt.subplots()
ax.hist(delivery_minutes, bins=6, edgecolor="white")
ax.set_title("Delivery times")
ax.set_xlabel("Minutes")
ax.set_ylabel("Number of deliveries")

Most deliveries take 19 to 33 minutes, so the tall bars are on the left. A few very slow deliveries make a tail to the right: this data is right-skewed.

Too few bins hide the shape; too many make it noisy. Try a couple of values.

Unlike a bar chart, the bars of a histogram touch, because the bins are next to each other on a number line.