You are reading an old version of the documentation (v3.1.3). For the latest version see
Version 3.1.3
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 0x7f188d4f7970>, <matplotlib.lines.Line2D object at 0x7f188c0dfa00>], 'caps': [<matplotlib.lines.Line2D object at 0x7f188c0df250>, <matplotlib.lines.Line2D object at 0x7f188c0dfa60>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f188d4f7af0>], 'medians': [<matplotlib.lines.Line2D object at 0x7f188c0df400>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f188d4f7370>], 'means': []}
fig2, ax2 = plt.subplots()
ax2.set_title('Notched boxes')
ax2.boxplot(data, notch=True)


{'whiskers': [<matplotlib.lines.Line2D object at 0x7f188d57a280>, <matplotlib.lines.Line2D object at 0x7f188d57a610>], 'caps': [<matplotlib.lines.Line2D object at 0x7f188d57a940>, <matplotlib.lines.Line2D object at 0x7f188d57ac70>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f188d57a2b0>], 'medians': [<matplotlib.lines.Line2D object at 0x7f188d5a5310>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f188d57a220>], '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 0x7f188d479400>, <matplotlib.lines.Line2D object at 0x7f188d479760>], 'caps': [<matplotlib.lines.Line2D object at 0x7f188d479a90>, <matplotlib.lines.Line2D object at 0x7f188d49f130>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f188d4793d0>], 'medians': [<matplotlib.lines.Line2D object at 0x7f188d479dc0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f188d479370>], 'means': []}
fig4, ax4 = plt.subplots()
ax4.set_title('Hide Outlier Points')
ax4.boxplot(data, showfliers=False)


{'whiskers': [<matplotlib.lines.Line2D object at 0x7f188d5d5130>, <matplotlib.lines.Line2D object at 0x7f188d5d5490>], 'caps': [<matplotlib.lines.Line2D object at 0x7f188d5d57c0>, <matplotlib.lines.Line2D object at 0x7f188d5d5af0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f188d5d5100>], 'medians': [<matplotlib.lines.Line2D object at 0x7f188d5b8190>], '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 0x7f188d5541c0>, <matplotlib.lines.Line2D object at 0x7f188d5544f0>], 'caps': [<matplotlib.lines.Line2D object at 0x7f188d554820>, <matplotlib.lines.Line2D object at 0x7f188d554b50>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f188d546df0>], 'medians': [<matplotlib.lines.Line2D object at 0x7f188d546e20>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f188d546d90>], '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 0x7f188d46fc40>, <matplotlib.lines.Line2D object at 0x7f188d611310>], 'caps': [<matplotlib.lines.Line2D object at 0x7f188d611640>, <matplotlib.lines.Line2D object at 0x7f188d611970>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f188d46fc10>], 'medians': [<matplotlib.lines.Line2D object at 0x7f188d46ffa0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f188d46fbb0>], '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 0x7f18a5a69af0>

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