You are reading an old version of the documentation (v3.1.1). For the latest version see
Version 3.1.2
Fork me on GitHub

Table of Contents

Related Topics

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))
fig1, ax1 = plt.subplots()
ax1.set_title('Basic Plot')


{'whiskers': [<matplotlib.lines.Line2D object at 0x7fb114c3df60>, <matplotlib.lines.Line2D object at 0x7fb114c3d048>], 'caps': [<matplotlib.lines.Line2D object at 0x7fb114c3d8d0>, <matplotlib.lines.Line2D object at 0x7fb114c13390>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fb114c3dcc0>], 'medians': [<matplotlib.lines.Line2D object at 0x7fb1146f4518>], 'fliers': [<matplotlib.lines.Line2D object at 0x7fb1146f42e8>], 'means': []}
fig2, ax2 = plt.subplots()
ax2.set_title('Notched boxes')
ax2.boxplot(data, notch=True)


{'whiskers': [<matplotlib.lines.Line2D object at 0x7fb113e887f0>, <matplotlib.lines.Line2D object at 0x7fb113e88ba8>], 'caps': [<matplotlib.lines.Line2D object at 0x7fb113e88ef0>, <matplotlib.lines.Line2D object at 0x7fb113e7e278>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fb113e886a0>], 'medians': [<matplotlib.lines.Line2D object at 0x7fb113e7e5c0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7fb113e7e908>], '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 0x7fb113c908d0>, <matplotlib.lines.Line2D object at 0x7fb113c90c88>], 'caps': [<matplotlib.lines.Line2D object at 0x7fb113c90fd0>, <matplotlib.lines.Line2D object at 0x7fb113db1358>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fb113c90780>], 'medians': [<matplotlib.lines.Line2D object at 0x7fb113db16a0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7fb113db19e8>], 'means': []}
fig4, ax4 = plt.subplots()
ax4.set_title('Hide Outlier Points')
ax4.boxplot(data, showfliers=False)


{'whiskers': [<matplotlib.lines.Line2D object at 0x7fb113b87518>, <matplotlib.lines.Line2D object at 0x7fb113b878d0>], 'caps': [<matplotlib.lines.Line2D object at 0x7fb113b87c18>, <matplotlib.lines.Line2D object at 0x7fb113b87f60>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fb113b873c8>], 'medians': [<matplotlib.lines.Line2D object at 0x7fb113b61160>], '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 0x7fb113d842e8>, <matplotlib.lines.Line2D object at 0x7fb113d846a0>], 'caps': [<matplotlib.lines.Line2D object at 0x7fb113d849e8>, <matplotlib.lines.Line2D object at 0x7fb113d84d30>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fb113d84198>], 'medians': [<matplotlib.lines.Line2D object at 0x7fb113d680b8>], 'fliers': [<matplotlib.lines.Line2D object at 0x7fb113d68400>], '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 0x7fb113e304a8>, <matplotlib.lines.Line2D object at 0x7fb113e31358>], 'caps': [<matplotlib.lines.Line2D object at 0x7fb113e316a0>, <matplotlib.lines.Line2D object at 0x7fb113e319e8>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fb113e305c0>], 'medians': [<matplotlib.lines.Line2D object at 0x7fb113e31d30>], 'fliers': [<matplotlib.lines.Line2D object at 0x7fb113e4b0b8>], '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:

import matplotlib


<function boxplot at 0x7fb11b15fd08>

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