Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #506

Boxplot Analysis

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

min Q1 (25th pct) median Q3 (75th pct) max (within 1.5×IQR) outlier

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

FeatureWhat It Tells You
Box height (IQR)How spread out the middle 50% of the data is
Line position within the boxSkew — median centered means symmetric; median near one edge means skewed
Whisker lengthHow far "normal" (non-outlier) values extend beyond the middle 50%
Dots beyond whiskersIndividual 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

BoxplotHistogram
Best forComparing a distribution across multiple groupsSeeing the detailed shape of one distribution
Shows multimodality?No — a boxplot can't reveal two separate peaksYes — directly visible
Shows outliers explicitly?Yes, marked individuallyNot directly, but visible as sparse bars in the tail
Compact for many groups?Very — many boxplots fit side by side cleanlyGets 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?

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →