Histogram
Python · Data Visualization
Theory
A histogram shows the distribution of one numeric variable. It splits the range of values into bins (intervals) and draws a bar for each bin whose height is how many values fall inside it. That reveals the shape of the data: where most values sit, how spread out they are, whether there is a long tail and whether there are outliers.
ax.hist(values, bins=10) chooses 10 equal-width bins between the minimum and maximum; you can also pass explicit edges such as bins=[0, 15, 30, 45, 60]. Each bin includes its left edge, and the last bin also includes the maximum. The number of bins changes the story: too few hide detail and too many look noisy. ax.hist returns the counts and bin edges, so you can print exactly what the chart shows; density=True scales heights so the total area is 1.
Why we use it
Averages hide shape. A histogram shows whether delivery times are tightly clustered or have a long tail of late orders, which tells you whether the mean is a fair summary and whether outliers need attention.
When to use it
Use a histogram for one continuous variable such as amounts, times or ages. Try a few bin counts before deciding. To compare distributions across groups, use box plots or overlapping histograms with alpha; for categories, use a bar chart instead.
How it works
- One numeric column
- Choose bins: a number or a list of edges
- Matplotlib counts values falling in each interval
- Does the shape look too blocky or too noisy? Yes: Change the number of bins and redraw. No: Keep the chart.
- Distribution: centre, spread, skew, outliers
Syntax
counts, edges, patches = ax.hist(values, bins=10)
ax.hist(values, bins=[0, 15, 30, 45, 60])
ax.hist(values, bins=20, density=True, alpha=0.6)Example
times = [18, 22, 25, 31, 34, 41, 55]
fig, ax = plt.subplots()
ax.hist(times, bins=4, edgecolor='white')Key points
- One numeric variable; bar height = count per bin
- bins=N gives N equal-width bins from min to max
- Pass a list of edges for meaningful ranges
- Too few bins hide shape, too many look noisy
- ax.hist returns counts and edges you can print