8000 Merge pull request #2474 from tonysyu/canonical-example-refactor · matplotlib/matplotlib@d81498f · GitHub
[go: up one dir, main page]

Skip to content

Commit d81498f

Browse files
committed
Merge pull request #2474 from tonysyu/canonical-example-refactor
MEP12: Example clean-up for reference
2 parents cc654db + ef51dc3 commit d81498f

File tree

5 files changed

+98
-123
lines changed

5 files changed

+98
-123
lines changed

examples/pylab_examples/histogram_demo_extended.py

Lines changed: 0 additions & 122 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Demo of the histogram (hist) function used to plot a cumulative distribution.
3+
4+
"""
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
from matplotlib import mlab
8+
9+
10+
mu = 200
11+
sigma = 25
12+
n_bins = 50
13+
x = mu + sigma*np.random.randn(10000)
14+
15+
n, bins, patches = plt.hist(x, n_bins, normed=1,
16+
histtype='step', cumulative=True)
17+
18+
# Add a line showing the expected distribution.
19+
y = mlab.normpdf(bins, mu, sigma).cumsum()
20+
y /= y[-1]
21+
plt.plot(bins, y, 'k--', linewidth=1.5)
22+
23+
# Overlay a reversed cumulative histogram.
24+
plt.hist(x, bins=bins, normed=1, histtype='step', cumulative=-1)
25+
26+
plt.grid(True)
27+
plt.ylim(0, 1.05)
28+
plt.title('cumulative step')
29+
30+
plt.show()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Demo of the histogram (hist) function with different ``histtype`` settings.
3+
4+
* Histogram with step curve that has a color fill.
5+
* Histogram with with unequal bin widths.
6+
7+
"""
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
11+
12+
mu = 200
13+
sigma = 25
14+
x = mu + sigma*np.random.randn(10000)
15+
16+
fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(8, 4))
17+
18+
ax0.hist(x, 20, normed=1, histtype='stepfilled', facecolor='g', alpha=0.75)
19+
ax0.set_title('stepfilled')
20+
21+
# Create a histogram by providing the bin edges (unequally spaced).
22+
bins = [100, 150, 180, 195, 205, 220, 250, 300]
23+
ax1.hist(x, bins, normed=1, histtype='bar', rwidth=0.8)
24+
ax1.set_title('unequal bins')
25+
26+
plt.tight_layout()
27+
plt.show()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Demo of the histogram (hist) function with multiple data sets.
3+
4+
Plot histogram with multiple sample sets and demonstrate:
5+
6+
* Use of legend with multiple sample sets
7+
* Stacked bars
8+
* Step curve with a color fill
9+
* Data sets of different sample sizes
10+
"""
11+
import numpy as np
12+
import matplotlib.pyplot as plt
13+
14+
15+
n_bins = 10
16+
x = np.random.randn(1000, 3)
17+
18+
fig, axes = plt.subplots(nrows=2, ncols=2)
19+
ax0, ax1, ax2, ax3 = axes.flat
20+
21+
colors = ['red', 'tan', 'lime']
22+
ax0.hist(x, n_bins, normed=1, histtype='bar', color=colors, label=colors)
23+
ax0.legend(prop={'size': 10})
24+
ax0.set_title('bars with legend')
25+
26+
ax1.hist(x, n_bins, normed=1, histtype='bar', stacked=True)
27+
ax1.set_title('stacked bar')
28+
29+
ax2.hist(x, n_bins, histtype='step', stacked=True, fill=True)
30+
ax2.set_title('st 6D40 epfilled')
31+
32+
# Make a multiple-histogram of data-sets with different length.
33+
x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]
34+
ax3.hist(x_multi, n_bins, histtype='bar')
35+
ax3.set_title('different sample sizes')
36+
37+
plt.tight_layout()
38+
plt.show()

examples/tests/backend_driver.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@
7474
files['statistics'] = [
7575
'errorbar_demo.py',
7676
'errorbar_demo_features.py',
77+
'histogram_demo_cumulative.py',
7778
'histogram_demo_features.py',
79+
'histogram_demo_histtypes.py',
80+
'histogram_demo_multihist.py',
7881
]
7982

8083
files['pie'] = [
@@ -172,7 +175,6 @@
172175
'hexbin_demo.py',
173176
'hexbin_demo2.py',
174177
'hist_colormapped.py',
175-
'histogram_demo_extended.py',
176178
'vline_hline_demo.py',
177179

178180
'image_clip_path.py',

0 commit comments

Comments
 (0)
0