|
4 | 4 | ================================================================
|
5 | 5 |
|
6 | 6 | * Histogram with step curve that has a color fill.
|
| 7 | +* Histogram with step curve with no fill. |
7 | 8 | * Histogram with custom and unequal bin widths.
|
| 9 | +* Two histograms with stacked bars. |
8 | 10 |
|
9 | 11 | Selecting different bin counts and sizes can significantly affect the
|
10 | 12 | shape of a histogram. The Astropy docs have a great section on how to
|
|
17 | 19 |
|
18 | 20 | np.random.seed(19680801)
|
19 | 21 |
|
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) |
23 | 25 |
|
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) |
25 | 29 |
|
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') |
28 | 43 |
|
29 | 44 | # Create a histogram by providing the bin edges (unequally spaced).
|
30 | 45 | 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') |
33 | 48 |
|
34 | 49 | fig.tight_layout()
|
35 | 50 | plt.show()
|
0 commit comments