Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a00932e

Browse files
authored
Merge pull request #7324 from phobson/boxplot-demo-update
DOC: Boxplot color demo update
2 parents 7bce844 + 11d9ec3 commit a00932e

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

examples/showcase/bachelors_degrees_by_gender.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
"""
2+
A graph of multiple time series which demonstrates extensive custom
3+
styling of plot frame, tick lines and labels, and line graph properties.
4+
5+
Also demonstrates the custom placement of text labels along the right edge
6+
as an alternative to a conventional legend.
7+
"""
8+
19
import matplotlib.pyplot as plt
210
from matplotlib.mlab import csv2rec
311
from matplotlib.cbook import get_sample_data
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
# Box plots with custom fill colors
1+
"""Box plots with custom fill colors.
2+
3+
This plot illustrates how to create two types of box plots
4+
(rectangular and notched), and how to fill them with custom
5+
colors by accessing the properties of the artists of the
6+
box plots. Additionally, the ``labels`` parameter is used to
7+
provide x-tick labels for each sample.
8+
"""
29

310
import matplotlib.pyplot as plt
411
import numpy as np
512

613
# Random test data
714
np.random.seed(123)
8-
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
15+
all_data = [np.random.normal(0, std, size=100) for std in range(1, 4)]
16+
labels = ['x1', 'x2', 'x3']
917

1018
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 5))
1119

1220
# rectangular box plot
1321
bplot1 = axes[0].boxplot(all_data,
14-
vert=True, # vertical box aligmnent
15-
patch_artist=True) # fill with color
22+
vert=True, # vertical box alignment
23+
patch_artist=True, # fill with color
24+
labels=labels) # will be used to label x-ticks
25+
axes[0].set_title('Rectangular box plot')
1626

1727
# notch shape box plot
1828
bplot2 = axes[1].boxplot(all_data,
1929
notch=True, # notch shape
20-
vert=True, # vertical box aligmnent
21-
patch_artist=True) # fill with color
30+
vert=True, # vertical box alignment
31+
patch_artist=True, # fill with color
32+
labels=labels) # will be used to label x-ticks
33+
axes[1].set_title('Notched box plot')
2234

2335
# fill with colors
2436
colors = ['pink', 'lightblue', 'lightgreen']
@@ -29,12 +41,7 @@
2941
# adding horizontal grid lines
3042
for ax in axes:
3143
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'])
44+
ax.set_xlabel('Three separate samples')
45+
ax.set_ylabel('Observed values')
3946

4047
plt.show()
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
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.
1+
"""Box plot - violin plot comparison.
102
3+
Note that although violin plots are closely related to Tukey's (1977) box
4+
plots, they add useful information such as the distribution of the sample
5+
data (density trace).
6+
7+
By default, box plots show data points outside 1.5 x the inter-quartile range
8+
as outliers above or below the whiskers whereas violin plots show the whole
9+
range of the data.
10+
11+
Violin plots require matplotlib >= 1.4.
12+
"""
1113
import matplotlib.pyplot as plt
1214
import numpy as np
1315

@@ -20,20 +22,20 @@
2022
axes[0].violinplot(all_data,
2123
showmeans=False,
2224
showmedians=True)
23-
axes[0].set_title('violin plot')
25+
axes[0].set_title('Violin plot')
2426

2527
# plot box plot
2628
axes[1].boxplot(all_data)
27-
axes[1].set_title('box plot')
29+
axes[1].set_title('Box plot')
2830

2931
# adding horizontal grid lines
3032
for ax in axes:
3133
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')
34+
ax.set_xticks([y + 1 for y in range(len(all_data))])
35+
ax.set_xlabel('Four separate samples')
36+
ax.set_ylabel('Observed values')
3537

3638
# add x-tick labels
37-
plt.setp(axes, xticks=[y+1 for y in range(len(all_data))],
39+
plt.setp(axes, xticks=[y + 1 for y in range(len(all_data))],
3840
xticklabels=['x1', 'x2', 'x3', 'x4'])
3941
plt.show()

0 commit comments

Comments
 (0)