Boxplots
Learn how to use boxplots to summarize distributions and compare groups.
Skill Level: Beginner
Prerequisites: Histograms
Estimated Time: 25 minutes
Story Time
In the previous lesson, you used histograms to study the distribution of one numerical variable.
Now you will learn another powerful chart for understanding distribution: the boxplot.
A boxplot gives a compact summary of the data and helps you quickly see the center, spread, and possible outliers. It is especially useful when you want to compare distributions across different groups.
In this lesson, you will learn how to create boxplots in Matplotlib and how to interpret what each part of the plot means.
Learning Objectives
By the end of this lesson, you will be able to:
- explain when a boxplot should be used
- create a boxplot with Matplotlib
- identify the median, quartiles, and whiskers
- interpret spread using the box and whiskers
- recognize possible outliers
- compare distributions across groups
Why This Topic Matters
In real data analysis, it is not enough to know only the average value.
Analysts also need to understand:
- how spread out the data is
- where the middle values lie
- whether the distribution is balanced or uneven
- whether unusual values appear
A boxplot helps answer these questions in a compact visual form. It is based on the five-number summary and is widely used in exploratory data analysis.
What Is a Boxplot?
A boxplot, also called a box-and-whisker plot, is a chart that summarizes the distribution of a numerical variable.
A boxplot usually shows:
- the median
- the first quartile (Q1)
- the third quartile (Q3)
- whiskers extending toward the lower and upper values
- possible outliers shown as individual points
This makes it easier to understand the overall distribution without showing every individual observation.
Understanding the Main Parts
Median
The line inside the box represents the median, which is the middle value of the data when sorted.
Quartiles
The bottom and top edges of the box represent:
- Q1: the 25th percentile
- Q3: the 75th percentile
This means the box contains the middle 50% of the data.
Interquartile Range
The distance from Q1 to Q3 is called the interquartile range (IQR).
It tells you how spread out the middle 50% of the data is. A wider box means greater spread in the middle portion of the distribution.
Whiskers
The whiskers extend beyond the box toward lower and higher values that are not considered outliers.
Outliers
Points that fall far outside the typical range may be shown individually as outliers. Boxplots often use the 1.5 × IQR rule to determine which points count as outliers.
When Should You Use a Boxplot?
Use a boxplot when:
- you want to summarize the distribution of a numerical variable
- you want to compare spread across groups
- you want to inspect median and quartiles
- you want to detect possible outliers
For example:
- compare math scores across gender groups
- compare reading scores across lunch categories
- compare salaries across departments
Boxplots are especially helpful when comparing several groups side by side.
Your First Boxplot
You can create a basic boxplot in Matplotlib with plt.boxplot().
import matplotlib.pyplot as plt
math_scores =[2][3][4][5][6][7][8][9][10][11]
plt.boxplot(math_scores)
plt.show()
This creates a single boxplot summarizing the distribution of mathematics scores.
Adding a Title and Axis Labels
import matplotlib.pyplot as plt;
math_scores =[3][4][5][6][7][8][9][10][11][2]
plt.boxplot(math_scores)
plt.title("Boxplot of Mathematics Scores")
plt.ylabel("Math Score")
plt.show()
The title and axis label make the plot easier to interpret.
Reading a Single Boxplot
When interpreting a single boxplot, ask:
- Where is the median?
- Is the box wide or narrow?
- Are the whiskers long or short?
- Are there any outliers?
- Is the median centered in the box or closer to one side?
For example, if the median is near the center of the box and the whiskers are fairly balanced, the distribution may be reasonably symmetric. If one whisker is much longer, the distribution may be skewed.
Example: Detecting an Outlier
import matplotlib.pyplot as plt
scores =[12][13][4][7][8][9][10][11][14][15][2]
plt.boxplot(scores)
plt.title("Boxplot with a Possible Outlier")
plt.ylabel("Score")
plt.show()
In this example, the value 98 may appear as a separate point beyond the whisker.
A strong interpretation would be:
- most scores are grouped in a lower range
- one value appears much higher than the rest
- that value may be a possible outlier
Comparing Groups with Boxplots
One of the biggest strengths of boxplots is side-by-side comparison.
import matplotlib.pyplot as plt
female_scores =[13][7][8][9][11][15][2][12]
male_scores =[16][4][5][8][10][11][17][3]
plt.boxplot([female_scores, male_scores], labels=["Female", "Male"])
plt.title("Math Scores by Gender")
plt.ylabel("Math Score")
plt.show()
This chart lets you compare:
- median score
- spread of the middle 50%
- overall range
- possible outliers
between the two groups.
Interpreting Group Comparison
Suppose the Male boxplot has a higher median line than the Female boxplot.
That suggests the median math score is higher for male students.
If one box is taller, that means the middle 50% of values are more spread out in that group. If one group has more outliers, that may indicate greater variability or unusual cases.
Boxplot vs Histogram
Both boxplots and histograms are used to study distributions, but they do so differently.
A histogram helps you see:
- shape
- peaks
- clustering
- approximate distribution form
A boxplot helps you see:
- median
- quartiles
- spread
- possible outliers
- group comparisons
Google Colab Activity
Using the Students Performance dataset, create a boxplot for mathematics score.
Then create side-by-side boxplots comparing mathematics score by gender.
Write 2–3 sentences that describe:
- which group has the higher median
- which group has greater spread
- whether any possible outliers appear
Practice in Colab
After completing the activity in Google Colab, mark it as complete below.
Practice Exercise
Create side-by-side boxplots for reading score by lunch type.
Then answer:
- Which group has the higher median?
- Which group has the wider box?
- Are there any possible outliers?
Check Your Understanding
1. What does the line inside the box of a boxplot represent?
2. What does the box itself usually represent?
3. What is the interquartile range (IQR)?
4. Why are boxplots especially useful?
5. What might a point beyond the whiskers represent?
Challenge Exercise
Choose a numerical variable and a grouping variable from your dataset.
Create side-by-side boxplots that help compare the groups clearly.
Your chart should include:
- a clear title
- readable group labels
- a y-axis label
- a short written interpretation
Your interpretation should mention median, spread, and any unusual points.
Common Mistakes
Thinking the Box Shows All Data
The box only shows the middle 50% of the data, not the entire dataset.
Ignoring the Median
The median is one of the most important parts of the boxplot. It often provides the clearest comparison between groups.
Forgetting What Outliers Mean
An outlier is a possible unusual value, not automatic proof of an error. It should be investigated, not blindly removed.
Using a Boxplot Without Interpretation
Do not stop at identifying the box and whiskers. Explain what they suggest about center, spread, and group differences.
Key Takeaways
In this lesson, you learned how to:
- create boxplots with Matplotlib
- interpret the median, quartiles, whiskers, and possible outliers
- compare distributions across groups
- understand how boxplots differ from histograms
- use boxplots as a compact tool for exploratory analysis
Continue Your Journey
Next, you will learn Heatmaps, which help you visualize values across a grid and identify patterns more quickly.