Skip to content

Commit 466255e

Browse files
committed
Merge pull request #3736 from rasbt/boxplot_examples
DOC : Boxplot examples + explanation
2 parents 57fcef2 + e1280e0 commit 466255e

File tree

6 files changed

+102
-0
lines changed

6 files changed

+102
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#########################################
2+
# OS-specific temporary and backup files
3+
.DS_Store
4+
15
#########################################
26
# Editor temporary/working/backup files #
37
.#*
@@ -8,6 +12,7 @@
812
*.kdev4
913
.project
1014
.pydevproject
15+
.swp
1116

1217
# Compiled source #
1318
###################

doc/.DS_Store

10 KB
Binary file not shown.

doc/_static/boxplot_explanation.png

186 KB
Loading

doc/faq/howto_faq.rst

+12
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,18 @@ though we have made significant progress towards supporting blocking events.
478478
able to create new figures and raise them in a subsequent call to
479479
``show`` after closing the figures from a previous call to ``show``.
480480

481+
.. _howto-boxplot_violinplot:
482+
483+
Interpreting box plots and violin plots
484+
-----------------------------------
485+
486+
Tukey's `box plots <http://matplotlib.org/examples/pylab_examples/boxplot_demo.html>`_ (Robert McGill, John W. Tukey and Wayne A. Larsen: "The American Statistician" Vol. 32, No. 1, Feb., 1978, pp. 12-16) are statistical plots that provide useful information about the data distribution such as skewness. However, bar plots with error bars are still the common standard in most scientific literature, and thus, the interpretation of box plots can be challenging for the unfamiliar reader. The figure below illustrates the different visual features of a box plot.
487+
488+
.. figure:: ../_static/boxplot_explanation.png
489+
490+
`Violin plots <http://matplotlib.org/examples/statistics/violinplot_demo.html>`_ are closely related to box plots but add useful information such as the distribution of the sample data (density trace).
491+
Violin plots were added in matplotlib 1.4.
492+
481493

482494
.. _howto-contribute:
483495

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Box plots with custom fill colors
2+
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
6+
# Random test data
7+
np.random.seed(123)
8+
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
9+
10+
fig, axes = plt.subplots(nrows=1,ncols=2, figsize=(12,5))
11+
12+
# rectangular box plot
13+
bplot1 = axes[0].boxplot(all_data,
14+
vert=True, # vertical box aligmnent
15+
patch_artist=True) # fill with color
16+
17+
# notch shape box plot
18+
bplot2 = axes[1].boxplot(all_data,
19+
notch=True, # notch shape
20+
vert=True, # vertical box aligmnent
21+
patch_artist=True) # fill with color
22+
23+
# fill with colors
24+
colors = ['pink', 'lightblue', 'lightgreen']
25+
for bplot in (bplot1, bplot2):
26+
for patch, color in zip(bplot['boxes'], colors):
27+
patch.set_facecolor(color)
28+
29+
# adding horizontal grid lines
30+
for ax in axes:
31+
ax.yaxis.grid(True)
32+
ax.set_xticks([y+1 for y in range(len(all_data))], )
33+
ax.set_xlabel('xlabel')
34+
ax.set_ylabel('ylabel')
35+
36+
# add x-tick labels
37+
plt.setp(axes, xticks=[y+1 for y in range(len(all_data))],
38+
xticklabels=['x1', 'x2', 'x3', 'x4'],
39+
)
40+
41+
plt.show()
42+
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Box plot - violin plot comparison
2+
#
3+
# Note that although violin plots are closely related to Tukey's (1977) box plots,
4+
# they add useful information such as the distribution of the sample data (density trace).
5+
#
6+
# By default, box plots show data points outside 1.5 x the inter-quartile range as outliers
7+
# above or below the whiskers wheras violin plots show the whole range of the data.
8+
#
9+
# Violin plots require matplotlib >= 1.4.
10+
11+
import matplotlib.pyplot as plt
12+
import numpy as np
13+
14+
fig, axes = plt.subplots(nrows=1,ncols=2, figsize=(12,5))
15+
16+
# generate some random test data
17+
all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]
18+
19+
# plot violin plot
20+
axes[0].violinplot(all_data,
21+
showmeans=False,
22+
showmedians=True
23+
)
24+
axes[0].set_title('violin plot')
25+
26+
# plot box plot
27+
axes[1].boxplot(all_data)
28+
axes[1].set_title('box plot')
29+
30+
# adding horizontal grid lines
31+
for ax in axes:
32+
ax.yaxis.grid(True)
33+
ax.set_xticks([y+1 for y in range(len(all_data))], )
34+
ax.set_xlabel('xlabel')
35+
ax.set_ylabel('ylabel')
36+
37+
# add x-tick labels
38+
plt.setp(axes, xticks=[y+1 for y in range(len(all_data))],
39+
xticklabels=['x1', 'x2', 'x3', 'x4'],
40+
)
41+
42+
plt.show()

0 commit comments

Comments
 (0)