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))
Basic Plot


{'whiskers': [<matplotlib.lines.Line2D object at 0x7f5f09b5e970>, <matplotlib.lines.Line2D object at 0x7f5f09af5e80>], 'caps': [<matplotlib.lines.Line2D object at 0x7f5f09af5c10>, <matplotlib.lines.Line2D object at 0x7f5f09c68d60>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f5f098d4580>], 'medians': [<matplotlib.lines.Line2D object at 0x7f5f09c68c10>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f5f09c68bb0>], 'means': []}
fig2, ax2 = plt.subplots()
ax2.set_title('Notched boxes')
ax2.boxplot(data, notch=True)
Notched boxes


{'whiskers': [<matplotlib.lines.Line2D object at 0x7f5f09272640>, <matplotlib.lines.Line2D object at 0x7f5f09272cd0>], 'caps': [<matplotlib.lines.Line2D object at 0x7f5f09272250>, <matplotlib.lines.Line2D object at 0x7f5f29300e20>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f5f09b93fd0>], 'medians': [<matplotlib.lines.Line2D object at 0x7f5f09b5c580>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f5f09b5c5b0>], '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 0x7f5f09f460a0>, <matplotlib.lines.Line2D object at 0x7f5f09f46580>], 'caps': [<matplotlib.lines.Line2D object at 0x7f5f09f43490>, <matplotlib.lines.Line2D object at 0x7f5f09f43820>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f5f09c5c760>], 'medians': [<matplotlib.lines.Line2D object at 0x7f5f09f26430>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f5f09f26790>], '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 0x7f5f092a7ca0>, <matplotlib.lines.Line2D object at 0x7f5f09259070>], 'caps': [<matplotlib.lines.Line2D object at 0x7f5f09259400>, <matplotlib.lines.Line2D object at 0x7f5f09259790>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f5f092a7910>], 'medians': [<matplotlib.lines.Line2D object at 0x7f5f09259b20>], '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 0x7f5f09c6e8e0>, <matplotlib.lines.Line2D object at 0x7f5f09c6ec70>], 'caps': [<matplotlib.lines.Line2D object at 0x7f5f09c73040>, <matplotlib.lines.Line2D object at 0x7f5f09c733d0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f5f09c6e550>], 'medians': [<matplotlib.lines.Line2D object at 0x7f5f09c73760>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f5f09c73ac0>], '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 0x7f5f09d51640>, <matplotlib.lines.Line2D object at 0x7f5f09d519d0>], 'caps': [<matplotlib.lines.Line2D object at 0x7f5f09d51d60>, <matplotlib.lines.Line2D object at 0x7f5f09911130>], 'boxes': [<matplotlib.lines.Line2D object at 0x7f5f09d512b0>], 'medians': [<matplotlib.lines.Line2D object at 0x7f5f099114c0>], 'fliers': [<matplotlib.lines.Line2D object at 0x7f5f09911820>], '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')
Multiple Samples with Different sizes


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


<function boxplot at 0x7f5f32b86ee0>

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

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