Boxplot
What is a Boxplot?
A boxplot divides the given data into 4 equal parts (quartiles) and summarizes
a dataset using 5-number summary:
• Minimum
• Q1 (First Quartile)
• Median (Q2)
• Q3 (Third Quartile)
• Maximum
Fig: 1 5-Number summary in the boxplot
Fig: 2 5-Number summary in the boxplot
Fig: 3 5-Numbers Q0, Q1, Q2, Q3, Q4 in boxplot
Fig: 3 Range, IQR, outliers in the boxplot
Why do we need boxplot? (uses)
To understand
(i) Spread (of the given dataset)
Spread ∝ Q3−Q1
(ii) Skewness (of the given dataset)
If median is in centre of boxplot – data distribution is symmetric
If median is shifted – data distribution is skewed
(iii) Outliers (in the given dataset)
Points outside whiskers are unusual data. They may be removed if
needed.
Mechanism:
Example Dataset:
[2, 4, 5, 7, 8, 10, 12, 15, 18]
Step 1: Sort the Data (if not already sorted)
Step 2: Calculate the Median (Q2), Q1, Q3 values and note the 5-point
summary
Calculate the Median (Q2) value
Q2 = value at (n+1)/2 position
(n+1)/2 = (9+1)/2 = 5th position
Therefore, Q2 = 8
Calculate First Quartile (Q1)
Median of lower half
Lower half: [2, 4, 5, 7]
Q1 = middle values of lower half /2
Q1 = (4+5)/2 = 4.5
Calculate Third Quartile (Q3)
(i) Median of upper half – Here, upper half is [10, 12, 15, 18]
Q3 = middle values of upper half /2
Q3 = (12+15)/2 = 13.5
Draw the Final 5-Number Summary
Measure Formula Value
Min smallest value in the dataset 2
Q1 median of lower half 4.5
Median (Q2) (n+1)/2 8
Q3 median of upper half 13.5
Max largest value in the dataset 18
Step 3: Find the Outliers
Calculate
Interquartile IQR = Q3 – Q1 = 13.5 – 4.5 = 9
Range (IQR)
Lower Limit = Q1−1.5 × IQR
= 4.5 – 1.5*9
= 4.5 – 13.5
Calculate lower- = -9
limit, upper-limit Upper Limit = Q3+1.5 × IQR
= 13.5 + 1.5*9
= 13.5 + 13.5
= 27
x< lower limit
Outlier Rule
x > upper limit
where, x – is a datapoint in the given dataset
Code:
""" Vertical boxplot """
marks = [2, 4, 5, 7, 8, 10, 12, 15, 18]
[Link](marks)
[Link]("Values")
[Link]('Boxplot example')
[Link]()
What we understand from this boxplot?
Box → from Q1 (4.5) to Q3 (13.5)
Line → Median (8)
Whiskers → 2 to 18
No outliers
""" Horizontal boxplot """
marks = [2, 4, 5, 7, 8, 10, 12, 15, 18]
[Link](marks, vert=False)
[Link]("Values")
[Link]('Boxplot example')
[Link]()
Example 2: With outlier
""" Boxplot """
import [Link] as pl
marks = [2, 4, 5, 7, 8, 10, 12, 15, 100]
[Link](marks, vert=False,
patch_artist=True) # fill color
[Link]("Values")
[Link]('Boxplot example')
[Link]()
Here,
data-point 100 is the outlier
How the 5-points, IQR, lower limit, higher limits are calculated for this dataset
[2, 4, 5, 7, 8, 10, 12, 15, 100]?
Step 1: Sort the Data (if not already sorted)
Step 2: Calculate the Median (Q2), Q1, Q3 values and note the 5-point
summary
Calculate the Median (Q2) value
Q2 = value at (n+1)/2 position
(n+1)/2 = (9+1)/2 = 5th position
Therefore, Q2 = 8
Calculate First Quartile (Q1)
Median of lower half
Lower half: [2, 4, 5, 7]
Q1 = middle values of lower half /2
Q1 = (4+5)/2 = 4.5
Calculate Third Quartile (Q3)
(i) Median of upper half – Here, upper half is [10, 12, 15, 100]
Q3 = middle values of upper half /2
Q3 = (12+15)/2 = 13.5
Draw the Final 5-Number Summary
Measure Formula Value
Min smallest value in the dataset 2
Q1 median of lower half 4.5
Median (Q2) (n+1)/2 8
Q3 median of upper half 13.5
Max largest value in the dataset 100
Step 3: Find the Outliers
Calculate
Interquartile IQR = Q3 – Q1 = 13.5 – 4.5 = 9
Range (IQR)
Lower Limit = Q1−1.5 × IQR
= 4.5 – 1.5*9
= 4.5 – 13.5
Calculate lower- = -9
limit, upper-limit Upper Limit = Q3+1.5 × IQR
= 13.5 + 1.5*9
= 13.5 + 13.5
= 27
x< lower limit
x > upper limit
where, x – is a datapoint in the given dataset
Outlier Rule
In this dataset,
(i) No value less than -9 is present.
(ii) But, value greater than 27 is present. It is 100.
So, 100 is the outlier.