Box plot in matplotlib

The boxplot function in matplotlib

When using matplotlib you can use the boxplot function to create a box plot, as in the example below. Note that in order to make all the examples reproducible we have set a seed and generated a variable named x based on a normal distribution.

import numpy as np
import matplotlib.pyplot as plt

# Seed for reproducibility
np.random.seed(5)

# Data simulation
x = np.random.normal(0, 1, 200)

# Plot
fig, ax = plt.subplots()
ax.boxplot(x)
# plt.show()

Basic box plot in matplotlib plt

Horizontal box plot

The boxplot function provides several arguments to customize the default box plot. In case you want to create an horizontal box plot rather than vertical you can just set the vert argument to False.

import numpy as np
import matplotlib.pyplot as plt

# Seed for reproducibility
np.random.seed(5)

# Data simulation
x = np.random.normal(0, 1, 200)

# Horizontal box plot
fig, ax = plt.subplots()
ax.boxplot(x, vert = False)
# plt.show()

Horizontal box plot in matplotlib

Confidence interval for the median (notch)

If you want to show the 95% confidence interval for the median you can set notch = True, as the interval be represented with ‘notches’ on the box.

import numpy as np
import matplotlib.pyplot as plt

# Seed for reproducibility
np.random.seed(5)

# Data simulation
x = np.random.normal(0, 1, 200)

# Box plot with notch
fig, ax = plt.subplots()
ax.boxplot(x, notch = True)
# plt.show()

Box plot with notch (confidence intervals) in Python

Outliers symbol

The default symbol for representing the outliers (or fliers) are circles. However, you can customize the marker and its color passing a dictionary to the flierprops argument, as shown below.

import numpy as np
import matplotlib.pyplot as plt

# Seed for reproducibility
np.random.seed(5)

# Data simulation
x = np.random.normal(0, 1, 200)

# Box plot
fig, ax = plt.subplots()
ax.boxplot(x, flierprops = dict(marker = "s", markerfacecolor = "red"))
# plt.show()

Change the style of the fliers or ourliers of a box plot in matplotlib

Box plot with mean

The central line of a box plot usually is the median of the data. Nonetheless, if you set the showmeans argument to True the mean will be also represented with a green triangle, and if you also set meanline = True the mean will be represented with a dashed line (the line will be green by default).

import numpy as np
import matplotlib.pyplot as plt

# Seed for reproducibility
np.random.seed(5)

# Data simulation
x = np.random.normal(0, 1, 200)

# Box plot
fig, ax = plt.subplots()
ax.boxplot(x, showmeans = True, meanline = True)
# plt.show()

Box plot with mean in Python

Remove outliers

If you want to remove the outliers (also known as fliers) from the plot, you can set the showfliers argument to False.

import numpy as np
import matplotlib.pyplot as plt

# Seed for reproducibility
np.random.seed(5)

# Data simulation
x = np.random.normal(0, 1, 200)

# Box plot without outliers
fig, ax = plt.subplots()
ax.boxplot(x, showfliers = False)
# plt.show()

Remove the outliers of a box plot in Python

Color customization

Fill color

The fill color of a matplotlib box plot is white by default, but you can override it setting patch_artist = True and passing a dict with facecolor to the boxprops argument, as in the following example.

Fill color of a box plot in matplotlib

import numpy as np
import matplotlib.pyplot as plt

# Seed for reproducibility
np.random.seed(5)

# Data simulation
x = np.random.normal(0, 1, 200)

# Box plot fill color
fig, ax = plt.subplots()
ax.boxplot(x,
           patch_artist = True,
           boxprops = dict(facecolor = "lightblue"))
# plt.show()

If you don’t set patch_artist = True you will get an error saying: “AttributeError: ‘Line2D’ object has no property ‘facecolor’”

Median line color

The default color for the median line is orange. However, you can change it passing a dict to medianprops. Note that you can also modify the width of the line this way.

Median line color of the matplotlib boxplot function

import numpy as np
import matplotlib.pyplot as plt

# Seed for reproducibility
np.random.seed(5)

# Data simulation
x = np.random.normal(0, 1, 200)

# Box plot
fig, ax = plt.subplots()
plt.boxplot(x, medianprops = dict(color = "green", linewidth = 1.5))
# plt.show()

whiskers color

You can also customize the color of the whiskers of the box plot. You just need to pass a dict with a color to whiskerprops.

Custom whiskers color in matplotlib

import numpy as np
import matplotlib.pyplot as plt

# Seed for reproducibility
np.random.seed(5)

# Data simulation
x = np.random.normal(0, 1, 200)

# Box plot
fig, ax = plt.subplots()
plt.boxplot(x, whiskerprops = dict(color = "red", linewidth = 2))
# plt.show()

Whiskers caps color

Finally, you can also set the color not only for the whiskers, but for the whiskers caps with capprops.

Change the color of the whiskers caps in matplotlib

import numpy as np
import matplotlib.pyplot as plt

# Seed for reproducibility
np.random.seed(5)

# Data simulation
x = np.random.normal(0, 1, 200)

# Box plot
fig, ax = plt.subplots()
plt.boxplot(x, capprops = dict(color = "red", linewidth = 2))
# plt.show()
Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon

See also