Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Box Plots

DS 2023 | Communicating with Data

box_plot.png

Overview

A box plot is a way to display the summary statistics of a numeric feature.

It uses a minimalist geometric drawing composed of a set of glyphs — a rectangle (the “box”), perpendicular lines (the “whiskers”), and small circles — plotted along a line.

The line is metric, such that distance maps onto a numeric scale, and the position of the glyphs along the axis signifies their numeric values.

Because of its design, box plots are often called “box-and-whiskers” plots.

Using the symbolism of the image, a box plot represents the following statistics:

StatisticLocation on box plot
Minimum value (min)Far left whisker
First quartile (Q1)Far left box border
Median valueCentral line
Third quartile (Q3)Far right box border
Maximum value (max)Far right whisker
Interquartile range (IQR)The box
OutliersDots to the left and right of Q1 and Q3

Use Cases

Box plots play a central role in exploratory data analysis (EDA).

They can quickly show how much a distribution varies from the normal.

They are also optimized for showing outliers — in fact, the minimum and maximum values represented by the outer whiskers are estimates of outlier thresholds.

Comparing many groups at once

This is the box plot’s principal advantage over both the histogram and the density plot.

A box plot occupies a single line, so twenty or fifty groups can be placed on one set of axes and still be read.

Twenty histograms require twenty panels. Twenty density curves on shared axes become an unreadable tangle.

Identifying outliers by an explicit rule

A histogram or a density plot leaves the identification of outliers to the reader’s judgment.

The box plot applies a stated rule, Tukey’s 1.5×IQR1.5 \times IQR, and marks every point that falls outside it.

This makes the determination reproducible, and it makes it arguable, which is better than leaving it implicit.

Reading summary statistics off the plot

The median, the quartiles, and the interquartile range can be read directly from their positions on the axis.

Neither a histogram nor a density plot displays any of these quantities.

Robustness

The box plot is built from quantiles, which are resistant to extreme values.

Adding a single observation of 106 to a series moves the mean substantially and moves the median not at all. The box plot registers such a value as one outlier point rather than rescaling the whole display.

How outliers are computed

Each of the visual features of the box plot corresponds to a number based on the quantiles associated with a series. The order of computation is as follows:

  1. Compute Q1Q1, the median, and Q3Q3, i.e. the first, second, and third quantiles.

    • These values may be computed by hand but are easily obtained by tools like Pandas’ .quantile() and .median() methods.

  2. Compute the interquartile range IQRIQR:

    • IQR=Q3−Q1IQR = Q3 - Q1.

  3. Multiply the IQRIQR by a factor, which is usually 1.5 by Tukey’s rule:

    • tukey=IQR×1.5tukey = IQR \times 1.5

  4. Compute the maxmax and minmin values:

    • min=Q1−tukeymin = Q1 - tukey

    • max=Q3+tukeymax = Q3 + tukey

    • Replace these values with the nearest actual data points that do not exceed the value.

  5. Find outliers:

    • <min< min

    • >max> max

Creating a box plot from scratch

First, let’s create a function to compute the various stats required to construct a box plot from a series of numeric data.

Let’s try it out on the tips data set from Seaborn.

Loading...
Q1 13.3475 median 17.795 Q3 24.1275 IQR 10.78 min 3.07 max 40.17 outliers [48.27, 44.3, 41.19, 48.17, 50.81, 45.35, 40.5... dtype: object

Now let’s create a function to draw the box plot.

We will use straight Matplotlib here, i.e. the functions plt.subplots() and plt.plot().

The colors of each part of different to highlight the constructed nature of the plot.

<Figure size 1000x100 with 1 Axes>

Using Pandas’ box plot function

Of course, Pandas can do all this for you and more.

Here is the simplest example.

<Figure size 640x480 with 1 Axes>

We can make the plot horizontal by seting vert to False.

<Figure size 640x480 with 1 Axes>

We are of course also free to clean it up a bit. A single box plot doesn’t really need a bounding box.

<Figure size 500x200 with 1 Axes>

Showing box plots by category

Let’s look at another data set to show how box plots for multiple series can be plotted on the same axes.

Here we use the healthexp data set from Seaborn which tracks health costs and outcomes for various countries from 1970 to 2020.

Loading...

To plot all of these at once, in order to compare them, we can set the by parameter to Country.

This implicitly groups the data by Country and then shows the respective box plots on a single axes.

<Figure size 640x480 with 1 Axes>

Here is how we can achieve the same effect using wide data.

Loading...
<Figure size 640x480 with 1 Axes>

A horizontal view is sometimes preferable when the number of box plots is large, given the constraint of page width.

<Figure size 640x480 with 1 Axes>

Now, we can use subplots to compare the categories, but since box plots are relatively compact, there is no real value gained by doing this.

In fact, it complicates things. You have to remove things to make the visualization clearer.

<Figure size 1000x500 with 6 Axes>

Sorting by median

Often, it is useful to sort the box plots to show ranking. It is common practice to sort by median.

Here is a way to do that.

Index(['USA', 'Germany', 'Great Britain', 'Canada', 'France', 'Japan'], dtype='str', name='Country')
<Figure size 640x480 with 1 Axes>

Sorting by category

Sometimes, the categories used to group box plots have a natural order.

This is true of the day feature in the tips data set.

You want these to display in order.

But look what happens when you ask Pandas’ to group the plots by day.

<Figure size 640x480 with 1 Axes>

They display out of their natural order; instead, they are displayed in alphabetic order, which is not what we want.

This happens even though the data type associated with day in tips is Category, which preserves the correct order. However, the plot function does not honor this order.

The solution to this is to create a wide version of the data, i.e. we need to project the domain of day onto the column axis of a new data frame.

Because day is of type category, the pivot returns the columns in their defined order.

Loading...

Note the order of the days that order of days is what we want.

Now, we can pass the wide table to Pandas and it will do the right thing.

<Figure size 640x480 with 1 Axes>

Change the properties of the box plot image

There are various properties of box plots that can be changed by passing values to the boxprops dictionary.

As always, consult the documentation to learn about all the properties that can be changed.

<Figure size 640x480 with 1 Axes>

Combining box plots and histograms

Because box plots are minimalist in their design — which contributes to their usefulness — they are often complemented with histograms, so that the shape of the distribution can be viewed as well.

Here is one way to combine them.

<Figure size 640x480 with 2 Axes>

Another way is to juxtapose them vertically, using a horizontal box plot to match the x axis of the histogram.

<Figure size 640x480 with 2 Axes>
References
  1. Lin, H., Zhang, R., & Tong, T. (2025). When Tukey Meets Chauvenet: A New Boxplot Criterion for Outlier Detection. Journal of Computational and Graphical Statistics, 35(1), 198–211. 10.1080/10618600.2025.2520577