Box plot
A box plot summarizes a distribution's median, quartiles and outliers in one compact picture.
In this page:
Syntax
df.plot.box()
df.boxplot(column="value_column", by="group_column")
Box plot
df.plot.box() or df.boxplot(by=...) draws the median line, the box from Q1 to Q3, whiskers, and dots for outliers. It compares distributions across columns or groups at a glance.
Note:
Points beyond the whiskers are potential outliers.
Example: Box plot
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({"a": [10, 12, 11, 13, 12, 40], "b": [20, 22, 21, 23, 22, 24]})
ax = df.plot.box()
print("boxes:", len(ax.lines) // 6 if len(ax.lines) >= 12 else len(ax.lines))
print(df.describe().loc[["min", "50%", "max"]])
# Output:
# boxes: 2
# a b
# min 10.0 20.0
# 50% 12.0 22.0
# max 40.0 24.0
Related Topics
Common Mistakes
- Reading the box line as the mean
- Ignoring the whiskers definition
- Using it on tiny samples
Chapter Summary
- Box shows median and quartiles
- Whiskers show spread
- Dots mark outliers
- Compare groups side by side
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: