|
1 | 1 | """ |
2 | | -============= |
3 | | -Histline Demo |
4 | | -============= |
| 2 | +=========== |
| 3 | +Stairs Demo |
| 4 | +=========== |
5 | 5 |
|
6 | 6 | This example demonstrates the use of `~.matplotlib.pyplot.stairs` |
7 | 7 | for histogram and histogram-like data visualization and an associated |
8 | | -underlying `.StepPatch` artist, which is |
9 | | -a contrained version of `.PathPatch` specified by its bins and edges. |
| 8 | +underlying `.StepPatch` artist, which is a specialized version of |
| 9 | +`.PathPatch` specified by its bins and edges. |
| 10 | +
|
| 11 | +The primary difference to `~.matplotlib.pyplot.step` is that ``stairs`` |
| 12 | +x-like edges input is one longer than its y-like values input. |
10 | 13 | """ |
11 | 14 |
|
12 | 15 | import numpy as np |
|
19 | 22 |
|
20 | 23 | fig, axs = plt.subplots(3, 1, figsize=(7, 15)) |
21 | 24 | axs[0].stairs(h, bins, label='Simple histogram') |
22 | | -axs[0].stairs(h, bins+5, baseline=50, label='--//-- w/ modified baseline') |
23 | | -axs[0].stairs(h, bins+10, baseline=None, label='--//-- w/ no edges') |
| 25 | +axs[0].stairs(h, bins+5, baseline=50, label='Modified baseline') |
| 26 | +axs[0].stairs(h, bins+10, baseline=None, label='No edges') |
24 | 27 | axs[0].set_title("Step Histograms") |
25 | 28 |
|
26 | 29 | axs[1].stairs(np.arange(1, 6, 1), fill=True, |
27 | | - label='Filled histogram\nw/ automatatic edges') |
| 30 | + label='Filled histogram\nw/ automatic edges') |
28 | 31 | axs[1].stairs(np.arange(1, 6, 1)*0.3, np.arange(2, 8, 1), |
29 | 32 | orientation='horizontal', hatch='//', |
30 | 33 | label='Hatched histogram\nw/ horizontal orientation') |
|
43 | 46 | ax.legend() |
44 | 47 | plt.show() |
45 | 48 |
|
| 49 | +############################################################################# |
| 50 | +# Comparison of `.pyplot.step` and `.pyplot.stairs`. |
| 51 | + |
| 52 | +bins = np.arange(14) |
| 53 | +centers = bins[:-1] + np.diff(bins)/2 |
| 54 | +y = np.sin(centers / 2) |
| 55 | + |
| 56 | +plt.step(bins[:-1], y, where='post', label='Step(where="post")') |
| 57 | +plt.plot(bins[:-1], y, 'o--', color='grey', alpha=0.3) |
| 58 | + |
| 59 | +plt.stairs(y - 1, bins, baseline=None, label='Stairs') |
| 60 | +plt.plot(centers, y - 1, 'o--', color='grey', alpha=0.3) |
| 61 | +plt.plot(np.repeat(bins, 2), np.hstack([y[0], np.repeat(y, 2), y[-1]]) - 1, |
| 62 | + 'o', color='red', alpha=0.2) |
| 63 | + |
| 64 | +plt.legend() |
| 65 | +plt.title('plt.step vs plt.stairs') |
| 66 | +plt.show() |
46 | 67 |
|
47 | 68 | ############################################################################# |
48 | 69 | # |
|
0 commit comments