Box Plots
A box plot (or box-and-whisker plot) summarizes the distribution of a numeric variable using five key statistics - the minimum, first quartile, median, third quartile, and maximum - making it an efficient way to spot spread, skewness, and outliers at a glance.
Anatomy of a Box Plot
- Box - spans from the first quartile (Q1) to the third quartile (Q3), representing the middle 50% of the data (the interquartile range, or IQR)
- Line inside the box - marks the median (Q2)
- Whiskers - extend to the smallest and largest values within 1.5 times the IQR from the box
- Points beyond the whiskers - plotted individually as potential outliers
The IQR and Outlier Rule
IQR = Q3 - Q1
Lower bound = Q1 - 1.5 * IQR
Upper bound = Q3 + 1.5 * IQR
Any data point falling outside these bounds is typically flagged as a potential outlier.
Plotting in Python
import matplotlib.pyplot as plt
plt.boxplot(data, vert=True, patch_artist=True)
plt.ylabel("Value")
plt.title("Box Plot")
plt.show()
Comparing Groups
Box plots are especially useful for comparing distributions across multiple groups side by side - for example, comparing exam scores across several different classes in a single chart.
Coming Up Next
To explore the relationship between two continuous variables rather than the distribution of just one, the final chart type to cover is the Scatter Plot.
Working Out a Box Plot by Hand
Calculating the numbers once makes every box plot easier to read. Take ten daily delivery times in minutes: 12, 15, 17, 19, 21, 22, 24, 26, 29, 55.
- Median (Q2): the middle of an even count is the average of the 5th and 6th values, (21 + 22) / 2 = 21.5.
- Q1: using NumPy's default linear method, 17.5.
- Q3: by the same method, 25.5.
- IQR: 25.5 minus 17.5 = 8.
- Fences: lower = 17.5 minus 12 = 5.5, upper = 25.5 plus 12 = 37.5.
So the box runs from 17.5 to 25.5, the lower whisker stops at 12 and the upper whisker at 29, the largest value inside the fence. The 55-minute delivery sits beyond the upper fence and is drawn as a separate point. You can confirm the quartiles in code:
import numpy as np
times = np.array([12, 15, 17, 19, 21, 22, 24, 26, 29, 55])
q1, q2, q3 = np.percentile(times, [25, 50, 75])
print(q1, q2, q3) # 17.5 21.5 25.5
Different tools use slightly different quartile methods, so a calculator or Excel may give values a little different from NumPy on small datasets. That is normal, not a mistake.
Comparing Groups With Seaborn
Seaborn makes grouped box plots straightforward because it works directly with column names in a DataFrame:
import seaborn as sns
import matplotlib.pyplot as plt
sns.boxplot(data=orders, x="city", y="delivery_minutes")
plt.show()
# split each city further by order type
sns.boxplot(data=orders, x="city", y="delivery_minutes", hue="order_type")
plt.show()
# show the actual points on top of the boxes
ax = sns.boxplot(data=orders, x="city", y="delivery_minutes", showfliers=False)
sns.stripplot(data=orders, x="city", y="delivery_minutes",
color="black", size=3, alpha=0.4, ax=ax)
plt.show()
With Pandas alone, orders.boxplot(column="delivery_minutes", by="city") gives a similar chart. In the overlay example, showfliers=False hides the box plot's own outlier markers so each point is not drawn twice.
Reading Shape From the Box
- Median close to the bottom of the box, long upper whisker: the data is right-skewed, with a tail of high values. Delivery times and incomes often look like this.
- Median near the top, long lower whisker: left-skewed.
- Boxes that barely overlap between groups: a hint that the groups genuinely differ, worth testing statistically.
- A very short box: most values are tightly clustered, even if a few outliers stretch the chart.
Misreadings to Avoid
- Deleting every point beyond the whiskers. The 1.5 × IQR rule flags candidates; a 55-minute delivery may be a real event worth explaining, not an error.
- Comparing groups of very different sizes without noting it. A box from five values is far less reliable than one from five hundred.
- Forgetting units or axis labels, which makes the chart impossible to interpret on its own.