Note
Click here to download the full example code
Boxplot Demo¶
Example boxplot code
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
# fake up some data
spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low))
fig1, ax1 = plt.subplots()
ax1.set_title('Basic Plot')
ax1.boxplot(data)

Out:
{'whiskers': [<matplotlib.lines.Line2D object at 0x7feac794f280>, <matplotlib.lines.Line2D object at 0x7feac794f760>], 'caps': [<matplotlib.lines.Line2D object at 0x7feac794f670>, <matplotlib.lines.Line2D object at 0x7feac795e370>], 'boxes': [<matplotlib.lines.Line2D object at 0x7feac79368b0>], 'medians': [<matplotlib.lines.Line2D object at 0x7feac795e6a0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7feac795edf0>], 'means': []}
fig2, ax2 = plt.subplots()
ax2.set_title('Notched boxes')
ax2.boxplot(data, notch=True)

Out:
{'whiskers': [<matplotlib.lines.Line2D object at 0x7feac75c5a90>, <matplotlib.lines.Line2D object at 0x7feac7916670>], 'caps': [<matplotlib.lines.Line2D object at 0x7feac7916e80>, <matplotlib.lines.Line2D object at 0x7feac7916160>], 'boxes': [<matplotlib.lines.Line2D object at 0x7feac75c5c10>], 'medians': [<matplotlib.lines.Line2D object at 0x7feac79162b0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7feac7ac4a30>], 'means': []}
green_diamond = dict(markerfacecolor='g', marker='D')
fig3, ax3 = plt.subplots()
ax3.set_title('Changed Outlier Symbols')
ax3.boxplot(data, flierprops=green_diamond)

Out:
{'whiskers': [<matplotlib.lines.Line2D object at 0x7feacc7b6490>, <matplotlib.lines.Line2D object at 0x7feacc7b6550>], 'caps': [<matplotlib.lines.Line2D object at 0x7feac7b47400>, <matplotlib.lines.Line2D object at 0x7feac7b479a0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7feacc8d0040>], 'medians': [<matplotlib.lines.Line2D object at 0x7feac7b47a60>], 'fliers': [<matplotlib.lines.Line2D object at 0x7feac7b475b0>], 'means': []}
fig4, ax4 = plt.subplots()
ax4.set_title('Hide Outlier Points')
ax4.boxplot(data, showfliers=False)

Out:
{'whiskers': [<matplotlib.lines.Line2D object at 0x7feacc9fcd00>, <matplotlib.lines.Line2D object at 0x7feacc9fc4c0>], 'caps': [<matplotlib.lines.Line2D object at 0x7feacc9fcbb0>, <matplotlib.lines.Line2D object at 0x7feaccaa6c70>], 'boxes': [<matplotlib.lines.Line2D object at 0x7feacc9fc430>], 'medians': [<matplotlib.lines.Line2D object at 0x7feaccaa6820>], 'fliers': [], 'means': []}
red_square = dict(markerfacecolor='r', marker='s')
fig5, ax5 = plt.subplots()
ax5.set_title('Horizontal Boxes')
ax5.boxplot(data, vert=False, flierprops=red_square)

Out:
{'whiskers': [<matplotlib.lines.Line2D object at 0x7feac7b71640>, <matplotlib.lines.Line2D object at 0x7feac7b719a0>], 'caps': [<matplotlib.lines.Line2D object at 0x7feac7b71d00>, <matplotlib.lines.Line2D object at 0x7feac79090a0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7feac7b712e0>], 'medians': [<matplotlib.lines.Line2D object at 0x7feac79093d0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7feac79096d0>], 'means': []}
fig6, ax6 = plt.subplots()
ax6.set_title('Shorter Whisker Length')
ax6.boxplot(data, flierprops=red_square, vert=False, whis=0.75)

Out:
{'whiskers': [<matplotlib.lines.Line2D object at 0x7feacc3406a0>, <matplotlib.lines.Line2D object at 0x7feacc340a00>], 'caps': [<matplotlib.lines.Line2D object at 0x7feacc340d60>, <matplotlib.lines.Line2D object at 0x7feacc368100>], 'boxes': [<matplotlib.lines.Line2D object at 0x7feacc340340>], 'medians': [<matplotlib.lines.Line2D object at 0x7feacc368430>], 'fliers': [<matplotlib.lines.Line2D object at 0x7feacc368730>], 'means': []}
Fake up some more data
spread = np.random.rand(50) * 100
center = np.ones(25) * 40
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
d2 = np.concatenate((spread, center, flier_high, flier_low))
data.shape = (-1, 1)
d2.shape = (-1, 1)
Making a 2-D array only works if all the columns are the same length. If they are not, then use a list instead. This is actually more efficient because boxplot converts a 2-D array into a list of vectors internally anyway.

References¶
The use of the following functions, methods, classes and modules is shown in this example:
import matplotlib
matplotlib.axes.Axes.boxplot
matplotlib.pyplot.boxplot
Out:
<function boxplot at 0x7feacfbce4c0>
Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery