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))
Basic Plot
{'whiskers': [<matplotlib.lines.Line2D object at 0x7f71776d59c0>, <matplotlib.lines.Line2D object at 0x7f71776d6a70>], 'caps': [<matplotlib.lines.Line2D object at 0x7f71776d5000>, <matplotlib.lines.Line2D object at 0x7f71776d6350>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f71776d79a0>], 'medians': [<matplotlib.lines.Line2D object at 0x7f71776d71f0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f71776d7ac0>], 'means': []}
fig2, ax2 = plt.subplots()
ax2.set_title('Notched boxes')
ax2.boxplot(data, notch=True)
Notched boxes
{'whiskers': [<matplotlib.lines.Line2D object at 0x7f7177463bb0>, <matplotlib.lines.Line2D object at 0x7f7177463790>], 'caps': [<matplotlib.lines.Line2D object at 0x7f7177462ce0>, <matplotlib.lines.Line2D object at 0x7f7177462260>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f7177461f00>], 'medians': [<matplotlib.lines.Line2D object at 0x7f71774637c0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f7175dfbb50>], 'means': []}
green_diamond = dict(markerfacecolor='g', marker='D')
fig3, ax3 = plt.subplots()
ax3.set_title('Changed Outlier Symbols')
ax3.boxplot(data, flierprops=green_diamond)
Changed Outlier Symbols
{'whiskers': [<matplotlib.lines.Line2D object at 0x7f7175e58430>, <matplotlib.lines.Line2D object at 0x7f7175e583d0>], 'caps': [<matplotlib.lines.Line2D object at 0x7f7175e5b0d0>, <matplotlib.lines.Line2D object at 0x7f7175e581f0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f7175e59120>], 'medians': [<matplotlib.lines.Line2D object at 0x7f7175e5aa40>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f7175e5a470>], 'means': []}
fig4, ax4 = plt.subplots()
ax4.set_title('Hide Outlier Points')
ax4.boxplot(data, showfliers=False)
Hide Outlier Points
{'whiskers': [<matplotlib.lines.Line2D object at 0x7f71761b8250>, <matplotlib.lines.Line2D object at 0x7f71761bab90>], 'caps': [<matplotlib.lines.Line2D object at 0x7f71761bb970>, <matplotlib.lines.Line2D object at 0x7f71761bb7c0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f71761bace0>], 'medians': [<matplotlib.lines.Line2D object at 0x7f71761b8880>], '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)
Horizontal Boxes
{'whiskers': [<matplotlib.lines.Line2D object at 0x7f717780d840>, <matplotlib.lines.Line2D object at 0x7f717780f130>], 'caps': [<matplotlib.lines.Line2D object at 0x7f717780f310>, <matplotlib.lines.Line2D object at 0x7f717780f460>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f717780eb00>], 'medians': [<matplotlib.lines.Line2D object at 0x7f717780e2f0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f717780e500>], 'means': []}
fig6, ax6 = plt.subplots()
ax6.set_title('Shorter Whisker Length')
ax6.boxplot(data, flierprops=red_square, vert=False, whis=0.75)
Shorter Whisker Length
{'whiskers': [<matplotlib.lines.Line2D object at 0x7f7175d8aef0>, <matplotlib.lines.Line2D object at 0x7f7175d899f0>], 'caps': [<matplotlib.lines.Line2D object at 0x7f7175d88fd0>, <matplotlib.lines.Line2D object at 0x7f7175d8bbe0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f71761f8820>], 'medians': [<matplotlib.lines.Line2D object at 0x7f7175d8a920>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f7175d89000>], 'means': []}

Fake up some more data

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()
Multiple Samples with Different sizes

References

The use of the following functions, methods, classes and modules is shown in this example:

Total running time of the script: ( 0 minutes 1.831 seconds)

Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery