0
0
Matplotlibdata~10 mins

Box plot with plt.boxplot in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Box plot with plt.boxplot
Start with data array
Call plt.boxplot(data)
Calculate quartiles and median
Calculate whiskers and outliers
Draw box, whiskers, and points
Show plot with plt.show()
This flow shows how matplotlib takes data, calculates statistics, draws the box plot, and displays it.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt

data = [7, 15, 36, 39, 40, 41, 42, 43, 47, 49]
plt.boxplot(data)
plt.show()
This code creates and shows a box plot for the given data list.
Execution Table
StepActionValue/ResultExplanation
1Input data[7, 15, 36, 39, 40, 41, 42, 43, 47, 49]Data list provided to boxplot
2Calculate Q1 (25th percentile)36.75Lower quartile of data
3Calculate Median (50th percentile)40.5Middle value of data
4Calculate Q3 (75th percentile)42.75Upper quartile of data
5Calculate IQR6.0Q3 - Q1 = 42.75 - 36.75
6Calculate lower fence27.75Q1 - 1.5*IQR = 36.75 - 9
7Calculate upper fence51.75Q3 + 1.5*IQR = 42.75 + 9
8Determine whisker data points36 (low), 49 (high)Furthest data points within fence range
9Identify outliers7, 15Data points outside fences
10Draw box plotBox from 36.75 to 42.75, median at 40.5, whiskers to 36 & 49, outliers at 7 & 15Visual representation created
11Show plotPlot window opensUser sees the box plot
12EndExecution completePlot displayed successfully
💡 All steps completed, box plot displayed with outliers at 7 and 15.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9Final
data[7,15,36,39,40,41,42,43,47,49][7,15,36,39,40,41,42,43,47,49][7,15,36,39,40,41,42,43,47,49][7,15,36,39,40,41,42,43,47,49][7,15,36,39,40,41,42,43,47,49][7,15,36,39,40,41,42,43,47,49][7,15,36,39,40,41,42,43,47,49][7,15,36,39,40,41,42,43,47,49][7,15,36,39,40,41,42,43,47,49][7,15,36,39,40,41,42,43,47,49]
Q1None36.7536.7536.7536.7536.7536.7536.7536.7536.75
MedianNoneNone40.540.540.540.540.540.540.540.5
Q3NoneNoneNone42.7542.7542.7542.7542.7542.7542.75
IQRNoneNoneNoneNone6.06.06.06.06.06.0
Lower whiskerNoneNoneNoneNoneNone27.7527.7527.7527.7527.75
Upper whiskerNoneNoneNoneNoneNoneNone51.7551.7551.7551.75
Whisker pointsNoneNoneNoneNoneNoneNoneNone[36,49][36,49][36,49]
OutliersNoneNoneNoneNoneNoneNoneNoneNone[7,15][7,15]
Key Moments - 3 Insights
Why is the median not the middle number in the data list?
The median is the middle value after sorting the data. In this example, the median is calculated as the average of the 5th and 6th values (40 and 41), resulting in 40.5, as shown in execution_table step 3.
What determines the length of the whiskers in the box plot?
Whiskers extend to the smallest and largest data points within 1.5 times the IQR from Q1 and Q3. This is shown in steps 6, 7, and 8 of the execution_table.
Why are there outliers in this box plot?
Outliers are data points outside the fences (27.75 to 51.75). 7 and 15 are below the lower fence, as identified in step 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of Q3 after step 4?
A40.5
B37.25
C42.75
D49
💡 Hint
Check the 'Value/Result' column for step 4 in the execution_table.
At which step does the calculation of the interquartile range (IQR) occur?
AStep 5
BStep 7
CStep 3
DStep 9
💡 Hint
Look for the step where IQR is calculated in the execution_table.
If the data had a value of 60 added, how would the upper whisker change?
AIt would increase to include 60
BIt would stay the same at 51.75
CIt would decrease
DIt would become equal to Q3
💡 Hint
Refer to how whiskers are calculated as Q3 + 1.5*IQR in steps 7 and 8. 60 exceeds the new upper fence.
Concept Snapshot
plt.boxplot(data)
- Draws a box plot for the data list
- Calculates Q1=36.75, median=40.5, Q3=42.75
- Whiskers extend to 1.5*IQR=9 beyond quartiles (fences 27.75-51.75)
- Outliers (7,15) shown as points beyond whiskers
- Use plt.show() to display the plot
Full Transcript
This visual execution traces how matplotlib's plt.boxplot function works step-by-step. Starting with the input data list, it calculates the first quartile (Q1=36.75), median (40.5), and third quartile (Q3=42.75). Then it computes the interquartile range (IQR=6.0). The fences extend to 1.5 times the IQR (9.0) from Q1 and Q3 (27.75 to 51.75). Whiskers go to the extreme non-outlier points (36 and 49). Points outside (7, 15) are outliers. Finally, the box plot is drawn showing the box from Q1 to Q3 with a line at the median, whiskers, and outliers. The plot is displayed with plt.show(). This helps beginners see how the box plot summarizes data distribution visually.