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 0x7f79c9f23850>, <matplotlib.lines.Line2D object at 0x7f79c9ea9460>], 'caps': [<matplotlib.lines.Line2D object at 0x7f79c9ea9430>, <matplotlib.lines.Line2D object at 0x7f79c9ea9790>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f79c9f235b0>], 'medians': [<matplotlib.lines.Line2D object at 0x7f79c9ea9040>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f79c9ea92b0>], 'means': []}
fig2, ax2 = plt.subplots()
ax2.set_title('Notched boxes')
ax2.boxplot(data, notch=True)

Out:
{'whiskers': [<matplotlib.lines.Line2D object at 0x7f79caf36f70>, <matplotlib.lines.Line2D object at 0x7f79caf367f0>], 'caps': [<matplotlib.lines.Line2D object at 0x7f79ca084a90>, <matplotlib.lines.Line2D object at 0x7f79ca084c10>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f79caf36a00>], 'medians': [<matplotlib.lines.Line2D object at 0x7f79ca084fa0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f79ca084b50>], '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 0x7f79c9f39cd0>, <matplotlib.lines.Line2D object at 0x7f79caee33d0>], 'caps': [<matplotlib.lines.Line2D object at 0x7f79caee31f0>, <matplotlib.lines.Line2D object at 0x7f79caee3610>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f79c9f396a0>], 'medians': [<matplotlib.lines.Line2D object at 0x7f79caee3af0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f79caee3850>], 'means': []}
fig4, ax4 = plt.subplots()
ax4.set_title('Hide Outlier Points')
ax4.boxplot(data, showfliers=False)

Out:
{'whiskers': [<matplotlib.lines.Line2D object at 0x7f79cae93520>, <matplotlib.lines.Line2D object at 0x7f79cae93dc0>], 'caps': [<matplotlib.lines.Line2D object at 0x7f79cae872e0>, <matplotlib.lines.Line2D object at 0x7f79cae87640>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f79cae93220>], 'medians': [<matplotlib.lines.Line2D object at 0x7f79cae879a0>], '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 0x7f79c9ef7c70>, <matplotlib.lines.Line2D object at 0x7f79c9ef7fa0>], 'caps': [<matplotlib.lines.Line2D object at 0x7f79c9f0d340>, <matplotlib.lines.Line2D object at 0x7f79c9f0d6a0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f79c9ef7910>], 'medians': [<matplotlib.lines.Line2D object at 0x7f79c9f0da00>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f79c9f0dd60>], '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 0x7f79ca98bd60>, <matplotlib.lines.Line2D object at 0x7f79cae4d0d0>], 'caps': [<matplotlib.lines.Line2D object at 0x7f79cae4dd60>, <matplotlib.lines.Line2D object at 0x7f79cae4d400>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f79ca98ba00>], 'medians': [<matplotlib.lines.Line2D object at 0x7f79cae4d7f0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f79cae4d040>], '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))
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.
data = [data, d2, d2[::2]]
fig7, ax7 = plt.subplots()
ax7.set_title('Multiple Samples with Different sizes')
ax7.boxplot(data)
plt.show()

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 0x7f79f26d18b0>
Total running time of the script: ( 0 minutes 2.021 seconds)
Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery