Boxplot Demo

Example boxplot code

import numpy as np
import matplotlib.pyplot as plt

# Fixing random state for reproducibility

# 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))


{'whiskers': [<matplotlib.lines.Line2D object at 0x7fdbd0b43790>, <matplotlib.lines.Line2D object at 0x7fdbd0b434f0>], 'caps': [<matplotlib.lines.Line2D object at 0x7fdbd0b43e20>, <matplotlib.lines.Line2D object at 0x7fdba4f7a0a0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fdbd0b433d0>], 'medians': [<matplotlib.lines.Line2D object at 0x7fdba4f7a730>], 'fliers': [<matplotlib.lines.Line2D object at 0x7fdba4f7ac70>], 'means': []}
fig2, ax2 = plt.subplots()
ax2.set_title('Notched boxes')
ax2.boxplot(data, notch=True)


{'whiskers': [<matplotlib.lines.Line2D object at 0x7fdbbc60cca0>, <matplotlib.lines.Line2D object at 0x7fdba4fa1280>], 'caps': [<matplotlib.lines.Line2D object at 0x7fdba4fa1e20>, <matplotlib.lines.Line2D object at 0x7fdba4fa14f0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fdbbc60c550>], 'medians': [<matplotlib.lines.Line2D object at 0x7fdba4fa1610>], 'fliers': [<matplotlib.lines.Line2D object at 0x7fdbbc5b6220>], 'means': []}
green_diamond = dict(markerfacecolor='g', marker='D')
fig3, ax3 = plt.subplots()
ax3.set_title('Changed Outlier Symbols')
ax3.boxplot(data, flierprops=green_diamond)


{'whiskers': [<matplotlib.lines.Line2D object at 0x7fdbbc6c64c0>, <matplotlib.lines.Line2D object at 0x7fdbbc6c6820>], 'caps': [<matplotlib.lines.Line2D object at 0x7fdbbc6c6b80>, <matplotlib.lines.Line2D object at 0x7fdbbc6c6ee0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fdbbc6c6160>], 'medians': [<matplotlib.lines.Line2D object at 0x7fdbbc6d6250>], 'fliers': [<matplotlib.lines.Line2D object at 0x7fdbbc6d6550>], 'means': []}
fig4, ax4 = plt.subplots()
ax4.set_title('Hide Outlier Points')
ax4.boxplot(data, showfliers=False)


{'whiskers': [<matplotlib.lines.Line2D object at 0x7fdbbc617400>, <matplotlib.lines.Line2D object at 0x7fdbbc617760>], 'caps': [<matplotlib.lines.Line2D object at 0x7fdbbc617ac0>, <matplotlib.lines.Line2D object at 0x7fdbbc617e20>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fdbbc6170a0>], 'medians': [<matplotlib.lines.Line2D object at 0x7fdbbc600190>], '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)


{'whiskers': [<matplotlib.lines.Line2D object at 0x7fdbbc63d370>, <matplotlib.lines.Line2D object at 0x7fdbbc63d6d0>], 'caps': [<matplotlib.lines.Line2D object at 0x7fdbbc63da30>, <matplotlib.lines.Line2D object at 0x7fdbbc63dd90>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fdbbc6391c0>], 'medians': [<matplotlib.lines.Line2D object at 0x7fdbbc657100>], 'fliers': [<matplotlib.lines.Line2D object at 0x7fdbbc657400>], 'means': []}
fig6, ax6 = plt.subplots()
ax6.set_title('Shorter Whisker Length')
ax6.boxplot(data, flierprops=red_square, vert=False, whis=0.75)


{'whiskers': [<matplotlib.lines.Line2D object at 0x7fdbbc72a3a0>, <matplotlib.lines.Line2D object at 0x7fdbbc72a700>], 'caps': [<matplotlib.lines.Line2D object at 0x7fdbbc72aa60>, <matplotlib.lines.Line2D object at 0x7fdbbc72adc0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fdbbc72a040>], 'medians': [<matplotlib.lines.Line2D object at 0x7fdbbc670130>], 'fliers': [<matplotlib.lines.Line2D object at 0x7fdbbc670430>], '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.

data = [data, d2, d2[::2, 0]]
fig7, ax7 = plt.subplots()
ax7.set_title('Multiple Samples with Different sizes')


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


<function boxplot at 0x7fdbbff761f0>

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