Box Plot
Python · Data Visualization
Theory
A box plot summarises a distribution with five numbers. The box runs from the first quartile (Q1, 25th percentile) to the third quartile (Q3, 75th percentile), so it holds the middle half of the data, and the line inside is the median. The box height is the interquartile range, IQR = Q3 - Q1.
The whiskers reach to the furthest data points within 1.5 x IQR of the box, and anything beyond is drawn as a separate dot: a likely outlier. For café bills [220, 250, 260, 280, 300, 310, 330, 360, 900], Q1 is 260, the median 300 and Q3 330, so the IQR is 70 and anything above 435 is flagged; the 900 bill shows as a lone dot. ax.boxplot([group_a, group_b], labels=[...]) puts several groups side by side, which makes box plots excellent for comparing distributions.
Why we use it
Box plots compare the centre, spread and outliers of several groups in one compact chart, and they flag unusual values with a clear rule rather than by eye.
When to use it
Use a box plot to compare a numeric variable across groups (salaries by department, delivery time by city) or to spot outliers. A histogram shows the full shape of one distribution better, including two peaks, which a box plot hides.
How it works
- Numeric values (one or more groups)
- Compute Q1, median and Q3
- IQR = Q3 - Q1; fences at Q1 - 1.5 IQR and Q3 + 1.5 IQR
- Is a point outside the fences? Yes: Draw it as an outlier dot. No: Whisker extends to it.
- Box, median line, whiskers and outliers per group
Syntax
bp = ax.boxplot([a, b, c], labels=['A', 'B', 'C'])
ax.boxplot(data, vert=False)
bp['medians'], bp['whiskers'], bp['fliers']Example
bills = [220, 250, 260, 280, 300, 900]
fig, ax = plt.subplots()
ax.boxplot(bills)Key points
- Box = Q1 to Q3 (middle 50%); line = median
- Whiskers stop at the last point within 1.5 x IQR
- Dots beyond the whiskers are potential outliers
- Great for comparing groups side by side
- Hides shape details such as two peaks; pair with a histogram