8000 DOC: added examples · matplotlib/matplotlib@9b97ac4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b97ac4

Browse files
committed
DOC: added examples
1 parent 5ded206 commit 9b97ac4

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

examples/statistics/boxplot_demo.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Demo of the new boxplot functionality
3+
"""
4+
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
8+
# fake data
9+
np.random.seed(937)
10+
data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
11+
labels = list('ABCD')
12+
fs = 10 # fontsize
13+
14+
# demonstrate how to toggle the display of different elements:
15+
fig, axes = plt.subplots(nrows=2, ncols=3)
16+
axes[0, 0].boxplot(data, labels=labels)
17+
axes[0, 0].set_title('Default', fontsize=fs)
18+
19+
axes[0, 1].boxplot(data, labels=labels, showmeans=True)
20+
axes[0, 1].set_title('showmeans=True', fontsize=fs)
21+
22+
axes[0, 2].boxplot(data, labels=labels, showmeans=True, meanline=True)
23+
axes[0, 2].set_title('showmeans=True, meanline=True', fontsize=fs)
24+
25+
axes[1, 0].boxplot(data, labels=labels, showbox=False, showcaps=False)
26+
axes[1, 0].set_title('Tufte Style (showbox=False, showcaps=False)', fontsize=fs)
27+
28+
axes[1, 1].boxplot(data, labels=labels, notch=True, bootstrap=10000)
29+
axes[1, 1].set_title('notch=True, bootstrap=10000', fontsize=fs)
30+
31+
axes[1, 2].boxplot(data, labels=labels, showfliers=False)
32+
axes[1, 2].set_title('showfliers=False', fontsize=fs)
33+
34+
for ax in axes.flatten():
35+
ax.set_yscale('log')
36+
ax.set_yticklabels([])
37+
38+
plt.show()
39+
40+
41+
# demonstrate how to customize the display different elements:
42+
boxprops = dict(linestyle='--', linewidth=3, color='darkgoldenrod')
43+
flierprops = dict(marker='o', markerfacecolor='green', markersize=12,
44+
linestyle='none')
45+
medianprops = dict(linestyle='-.', linewidth=2.5, color='firebrick')
46+
meanpointprops = dict(marker='D', markeredgecolor='black',
47+
markerfacecolor='firebrick')
48+
meanlineprops = dict(linestyle='--', linewidth=2.5, color='purple')
49+
50+
fig, axes = plt.subplots(nrows=2, ncols=3)
51+
axes[0, 0].boxplot(data, boxprops=boxprops)
52+
axes[0, 0].set_title('Custom boxprops', fontsize=fs)
53+
54+
axes[0, 1].boxplot(data, flierprops=flierprops, medianprops=medianprops)
55+
axes[0, 1].set_title('Custom medianprops and flierprops', fontsize=fs)
56+
57+
axes[0, 2].boxplot(data, whis='range')
58+
axes[0, 2].set_title('whis="range"', fontsize=fs)
59+
60+
axes[1, 0].boxplot(data, meanprops=meanpointprops, meanline=False,
61+
showmeans=True)
62+
axes[1, 0].set_title('Custom mean as point', fontsize=fs)
63+
64+
axes[1, 1].boxplot(data, meanprops=meanlineprops, meanline=True, showmeans=True)
65+
axes[1, 1].set_title('Custom mean as line', fontsize=fs)
66+
67+
axes[1, 2].boxplot(data, whis=[15, 85])
68+
axes[1, 2].set_title('whis=[15, 85] #percentiles', fontsize=fs)
69+
70+
for ax in axes.flatten():
71+
ax.set_yscale('log')
72+
ax.set_yticklabels([])
73+
74+
fig.suptitle("I never said they'd be pretty")
75+
plt.show()

examples/statistics/bxp_demo.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
Demo of the new boxplot drawer function
3+
"""
4+
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
import matplotlib.cbook as cbook
8+
9+
# fake data
10+
np.random.seed(937)
11+
data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
12+
labels = list('ABCD')
13+
14+
# compute the boxplot stats
15+
stats = cbook.boxplot_stats(data, labels=labels, bootstrap=10000)
16+
# After we've computed the stats, we can go through and change anything.
17+
# Just to prove it, I'll set the median of each set to the median of all
18+
# the data, and double the means
19+
for n in range(len(stats)):
20+
stats[n]['med'] = np.median(data)
21+
stats[n]['mean'] *= 2
22+
23+
print(stats[0].keys())
24+
fs = 10 # fontsize
25+
26+
# demonstrate how to toggle the display of different elements:
27+
fig, axes = plt.subplots(nrows=2, ncols=3)
28+
axes[0, 0].bxp(stats)
29+
axes[0, 0].set_title('Default', fontsize=fs)
30+
31+
axes[0, 1].bxp(stats, showmeans=True)
32+
axes[0, 1].set_title('showmeans=True', fontsize=fs)
33+
34+
axes[0, 2].bxp(stats, showmeans=True, meanline=True)
35+
axes[0, 2].set_title('showmeans=True, meanline=True', fontsize=fs)
36+
37+
axes[1, 0].bxp(stats, showbox=False, showcaps=False)
38+
axes[1, 0].set_title('Tufte Style (showbox=False, showcaps=False)', fontsize=fs)
39+
40+
axes[1, 1].bxp(stats, shownotches=True)
41+
axes[1, 1].set_title('notch=True', fontsize=fs)
42+
43+
axes[1, 2].bxp(stats, showfliers=False)
44+
axes[1, 2].set_title('showfliers=False', fontsize=fs)
45+
46+
for ax in axes.flatten():
47+
ax.set_yscale('log')
48+
ax.set_yticklabels([])
49+
50+
plt.show()
51+
52+
53+
# demonstrate how to customize the display different elements:
54+
boxprops = dict(linestyle='--', linewidth=3, color='darkgoldenrod')
55+
flierprops = dict(marker='o', markerfacecolor='green', markersize=12,
56+
linestyle='none')
57+
medianprops = dict(linestyle='-.', linewidth=2.5, color='firebrick')
58+
meanpointprops = dict(marker='D', markeredgecolor='black',
59+
markerfacecolor='firebrick')
60+
meanlineprops = dict(linestyle='--', linewidth=2.5, color='purple')
61+
62+
fig, axes = plt.subplots(nrows=2, ncols=2)
63+
axes[0, 0].bxp(stats, boxprops=boxprops)
64+
axes[0, 0].set_title('Custom boxprops', fontsize=fs)
65+
66+
axes[0, 1].bxp(stats, flierprops=flierprops, medianprops=medianprops)
67+
axes[0, 1].set_title('Custom medianprops and flierprops', fontsize=fs)
68+
69+
axes[1, 0].bxp(stats, meanprops=meanpointprops, meanline=False,
70+
showmeans=True)
71+
axes[1, 0].set_title('Custom mean as point', fontsize=fs)
72+
73+
axes[1, 1].bxp(stats, meanprops=meanlineprops, meanline=True, showmeans=True)
74+
axes[1, 1].set_title('Custom mean as line', fontsize=fs)
75+
76+
for ax in axes.flatten():
77+
ax.set_yscale('log')
78+
ax.set_yticklabels([])
79+
80+
fig.suptitle("I never said they'd be pretty")
81+
plt.show()

0 commit comments

Comments
 (0)
0