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

Skip to content

Commit 4c76e88

Browse files
meeseeksmachinetimhoffm
authored andcommitted
Backport PR #15357: Add 'step' and 'barstacked' to histogram_histtypes demo (#15377)
1 parent b983e5d commit 4c76e88

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

examples/statistics/histogram_histtypes.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
================================================================
55
66
* Histogram with step curve that has a color fill.
7+
* Histogram with step curve with no fill.
78
* Histogram with custom and unequal bin widths.
9+
* Two histograms with stacked bars.
810
911
Selecting different bin counts and sizes can significantly affect the
1012
shape of a histogram. The Astropy docs have a great section on how to
@@ -17,19 +19,32 @@
1719

1820
np.random.seed(19680801)
1921

20-
mu = 200
21-
sigma = 25
22-
x = np.random.normal(mu, sigma, size=100)
22+
mu_x = 200
23+
sigma_x = 25
24+
x = np.random.normal(mu_x, sigma_x, size=100)
2325

24-
fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(8, 4))
26+
mu_w = 200
27+
sigma_w = 10
28+
w = np.random.normal(mu_w, sigma_w, size=100)
2529

26-
ax0.hist(x, 20, density=True, histtype='stepfilled', facecolor='g', alpha=0.75)
27-
ax0.set_title('stepfilled')
30+
fig, axs = plt.subplots(nrows=2, ncols=2)
31+
32+
axs[0, 0].hist(x, 20, density=True, histtype='stepfilled', facecolor='g',
33+
alpha=0.75)
34+
axs[0, 0].set_title('stepfilled')
35+
36+
axs[0, 1].hist(x, 20, density=True, histtype='step', facecolor='g',
37+
alpha=0.75)
38+
axs[0, 1].set_title('step')
39+
40+
axs[1, 0].hist(x, density=True, histtype='barstacked', rwidth=0.8)
41+
axs[1, 0].hist(w, density=True, histtype='barstacked', rwidth=0.8)
42+
axs[1, 0].set_title('barstacked')
2843

2944
# Create a histogram by providing the bin edges (unequally spaced).
3045
bins = [100, 150, 180, 195, 205, 220, 250, 300]
31-
ax1.hist(x, bins, density=True, histtype='bar', rwidth=0.8)
32-
ax1.set_title('unequal bins')
46+
axs[1, 1].hist(x, bins, density=True, histtype='bar', rwidth=0.8)
47+
axs[1, 1].set_title('bar, unequal bins')
3348

3449
fig.tight_layout()
3550
plt.show()

0 commit comments

Comments
 (0)