Python Central HubSign in

Data Analytics with Python / Phase 6: Statistical Visualization with Seaborn

Box Plots and Whiskers

Use Seaborn box plots to compare distributions across categories and interpret quartiles and whiskers.

Box plot recap

Box plots show:

  • Median (middle line)
  • Q1 and Q3 (box)
  • Spread (IQR)
  • Outliers (points)

They are excellent for comparing a numeric variable across categories.

diagram From sorted values to a box plot mermaid
Quartiles split the sorted data into four equal chunks; the box, whiskers, and outlier dots are drawn from those cut points.

Seaborn boxplot

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
tips = sns.load_dataset("tips")
 
plt.figure(figsize=(7, 4))
sns.boxplot(data=tips, x="day", y="total_bill")
plt.title("Total bill by day")
plt.xlabel("Day")
plt.ylabel("Total bill")
plt.tight_layout()
plt.show()

Add hue for a second grouping

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
tips = sns.load_dataset("tips")
 
plt.figure(figsize=(7, 4))
sns.boxplot(data=tips, x="day", y="total_bill", hue="sex")
plt.title("Total bill by day and sex")
plt.tight_layout()
plt.show()

Visualize it

A box plot is a compact summary of a distribution's shape. The box spans Q1 to Q3 (the middle 50% of the data), the line inside is the median, the whiskers reach to the smallest/largest values still in range, and any dots beyond are outliers:

sketch Anatomy of a box plot p5.js
The box is Q1 to Q3, the inner line is the median, whiskers reach the in-range min/max, and dots beyond are outliers.

Tips

  • Treat outliers as signals, not always as errors.
  • If you need to see the raw points, add stripplot.

Check your understanding

quizCheck yourself
  1. Which pair of values do the edges of a box plot box mark?

    Show answer

    B — Q1 and Q3 — The box spans Q1 to Q3, the middle 50% of the data, and the line inside is the median.

  2. What do the dots drawn beyond the whiskers show?

    Show answer

    C — Outliers — Points beyond the whiskers are outliers. The lesson advises treating them as signals, not always as errors.

  3. Which call draws total_bill by day with a second split by sex?

    Show answer

    A — sns.boxplot(data=tips, x="day", y="total_bill", hue="sex") — hue adds the second grouping to boxplot. countplot only counts rows, and the other options drop the numeric y variable.

  4. You want to see the raw points on top of the box summary. What does the lesson suggest?

    Show answer

    C — Add stripplot — stripplot overlays individual observations, which a box plot hides.

Next

Continue to Violin Plots to see the full distribution shape, not just five summary numbers.

🧪 Try It Yourself

Exercise 1 – A Basic Box Plot

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Split by a Second Category

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Quartiles With Pandas

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did