A boxplot (box-and-whisker plot) compresses a numeric distribution's quartiles, spread and outliers into one compact, comparable shape — making it the fastest way to compare a feature's distribution across multiple groups at a glance.
Anatomy of a Boxplot
The box spans Q1 to Q3 (the middle 50% of the data — the IQR); the line inside is the median; whiskers extend to the most extreme non-outlier points; dots beyond are flagged outliers.
Reading a Boxplot — What Each Part Tells You
| Feature | What It Tells You |
|---|---|
| Box height (IQR) | How spread out the middle 50% of the data is |
| Line position within the box | Skew — median centered means symmetric; median near one edge means skewed |
| Whisker length | How far "normal" (non-outlier) values extend beyond the middle 50% |
| Dots beyond whiskers | Individual points flagged as outliers by the 1.5×IQR rule |
Python Implementation
import matplotlib.pyplot as plt
import seaborn as sns
# Single feature
plt.boxplot(df["price_lakh"])
plt.show()
# Comparing across groups — where boxplots are most powerful
sns.boxplot(x="city", y="price_lakh", data=df)
plt.show()
Worked Example — Comparing Three Groups
import pandas as pd
data = pd.DataFrame({
"city": ["Delhi"]*5 + ["Mumbai"]*5 + ["Pune"]*5,
"price_lakh": [45, 60, 55, 70, 65, 90, 110, 95, 130, 105, 50, 55, 48, 60, 52],
})
print(data.groupby("city")["price_lakh"].describe())
What the boxplots would show: Mumbai's box sits noticeably higher and wider than Delhi's or Pune's — both a higher typical price (median) and more variability (IQR) — while Delhi and Pune look more similar to each other. This is a much faster comparison than scanning three separate .describe() tables.
Boxplot vs Histogram — When to Use Each
| Boxplot | Histogram | |
|---|---|---|
| Best for | Comparing a distribution across multiple groups | Seeing the detailed shape of one distribution |
| Shows multimodality? | No — a boxplot can't reveal two separate peaks | Yes — directly visible |
| Shows outliers explicitly? | Yes, marked individually | Not directly, but visible as sparse bars in the tail |
| Compact for many groups? | Very — many boxplots fit side by side cleanly | Gets cluttered with more than 3-4 overlaid histograms |
Practical Use Cases
- Comparing a numeric feature's distribution across categories during bivariate analysis
- Quickly spotting outliers per group, rather than across the whole dataset at once
- Comparing a model's prediction errors across different segments to check for systematic bias in any one group
Common Mistakes
- Using a boxplot to check for multimodality — it structurally can't show two distinct peaks; use a histogram or KDE plot for that instead.
- Comparing boxplots across groups with very different sample sizes without noting that a group with very few points can produce a misleadingly clean-looking box.
- Assuming every dot beyond the whiskers is a data error — some are legitimate extreme (but real) values; always investigate before removing.
Interview Relevance
Q: "When would you use a boxplot instead of a histogram?" When comparing a numeric feature's distribution across multiple categories side by side — boxplots are far more compact and comparable than overlaying several histograms, though a histogram is better for examining one distribution's detailed shape (like multimodality).
Practice Question
You see a boxplot where the median line sits very close to the bottom edge of the box (near Q1), with a long whisker extending upward. What does this suggest about the data's skew?