Box plots

Seaborn basics

2 min read

Published Oct 7 2025


24
0
0
0

ChartsGraphsMatplotlibNumPyPandasPythonSeabornVisualisation

seaborn.boxplot() is used to visualise the distribution of a numerical variable and compare it across categories.

A box plot (a.k.a. box-and-whisker plot) shows:

  • The median (middle value)
  • The quartiles (Q1, Q3) — 25th and 75th percentiles
  • The interquartile range (IQR) — Q3 − Q1
  • Whiskers — roughly 1.5×IQR beyond the box
  • Outliers — data points outside whiskers

It’s great for spotting spread, symmetry, and outliers in your data.


Syntax:

sns.boxplot(
    data=None,
    x=None,
    y=None,
    hue=None,
    order=None,
    hue_order=None,
    orient=None,
    color=None,
    palette=None,
    width=0.8,
    dodge=True,
    showcaps=True,
    boxprops=None,
    whiskerprops=None,
    flierprops=None,
    medianprops=None,
    notch=False,
    **kwargs
)

Parameters:

  • data = DataFrame containing the data
  • x, y = Variables for categories and values
  • hue = Adds subcategories (side-by-side boxes)
  • order / hue_order = Specify category order
  • palette = Colour scheme
  • orient = "v" (vertical) or "h" (horizontal)
  • width = Width of boxes
  • dodge = Separate hue boxes side by side
  • notch = Draw notches to show confidence around median
  • flierprops = Customise outlier points
  • medianprops = Customise median line style




Basic example

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.boxplot(data=tips, x="day", y="total_bill")
plt.show()

Shows the distribution of total bills for each day.


seaborn box plot basic example





Add hue (subgroups)

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.boxplot(data=tips, x="day", y="total_bill", hue="sex")
plt.show()

  • Each day has two boxes, one per sex.
  • Allows you to compare distributions between men and women for each day.

seaborn box plot hue example





Horizontal boxes

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.boxplot(data=tips, x="total_bill", y="day", orient="h")
plt.show()

Flips the boxes horizontally — good for long category labels.


seaborn box plot horizontal example





Customise colors and style

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.boxplot(
    data=tips,
    x="day",
    y="total_bill",
    hue="sex",
    palette="pastel",
    width=0.6,
    boxprops={"edgecolor": "black"},
    medianprops={"color": "red", "linewidth": 2}
)
plt.show()

  • palette="pastel" → soft colour theme
  • medianprops → style the median line
  • boxprops → outline colour

seaborn box plot customise colour example





Show or hide outliers

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.boxplot(data=tips, x="day", y="total_bill", showfliers=False)
plt.show()

Removes outlier points (dots beyond whiskers).


seaborn box plot hide outliers example





Add notches for median confidence

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.boxplot(data=tips, x="day", y="total_bill", notch=True)
plt.show()

Adds notches to indicate an approximate 95% confidence interval around the median.


seaborn box plot add notch example





Control category order

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.boxplot(
    data=tips,
    x="day",
    y="total_bill",
    order=["Sun", "Sat", "Fri", "Thur"]
)
plt.show()

Controls the order of categories on the x-axis.


seaborn box plot order example





Using hue for subgroups

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.boxplot(
    data=tips,
    x="smoker",
    y="total_bill",
    hue="sex",
    palette="coolwarm"
)
plt.show()

Compares distributions of total bill across smokers/non-smokers for each sex.


seaborn box plot hue subgroup example
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact