|
3 | 3 | Stairs Demo |
4 | 4 | =========== |
5 | 5 |
|
6 | | -This example demonstrates the use of `~.matplotlib.pyplot.stairs` |
7 | | -for histogram and histogram-like data visualization and an associated |
8 | | -underlying `.StepPatch` artist, which is a specialized version of |
9 | | -`.PathPatch` specified by its bins and edges. |
| 6 | +This example demonstrates the use of `~.matplotlib.pyplot.stairs` for stepwise |
| 7 | +constant functions. A common use case is histogram and histogram-like data |
| 8 | +visualization. |
10 | 9 |
|
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. |
13 | 10 | """ |
14 | 11 |
|
15 | 12 | import numpy as np |
16 | 13 | import matplotlib.pyplot as plt |
17 | 14 | from matplotlib.patches import StepPatch |
18 | 15 |
|
19 | 16 | np.random.seed(0) |
20 | | -h, bins = np.histogram(np.random.normal(5, 3, 5000), |
21 | | - bins=np.linspace(0, 10, 20)) |
| 17 | +h, edges = np.histogram(np.random.normal(5, 3, 5000), |
| 18 | + bins=np.linspace(0, 10, 20)) |
22 | 19 |
|
23 | 20 | fig, axs = plt.subplots(3, 1, figsize=(7, 15)) |
24 | | -axs[0].stairs(h, bins, label='Simple histogram') |
25 | | -axs[0].stairs(h, bins+5, baseline=50, label='Modified baseline') |
26 | | -axs[0].stairs(h, bins+10, baseline=None, label='No edges') |
| 21 | +axs[0].stairs(h, edges, label='Simple histogram') |
| 22 | +axs[0].stairs(h, edges + 5, baseline=50, label='Modified baseline') |
| 23 | +axs[0].stairs(h, edges + 10, baseline=None, label='No edges') |
27 | 24 | axs[0].set_title("Step Histograms") |
28 | 25 |
|
29 | 26 | axs[1].stairs(np.arange(1, 6, 1), fill=True, |
|
47 | 44 | plt.show() |
48 | 45 |
|
49 | 46 | ############################################################################# |
50 | | -# Comparison of `.pyplot.step` and `.pyplot.stairs`. |
| 47 | +# Comparison of `.pyplot.step` and `.pyplot.stairs` |
| 48 | +# ------------------------------------------------- |
| 49 | +# |
| 50 | +# `.pyplot.step` defines the positions of the steps as single values. The steps |
| 51 | +# extend left/right/both ways from these reference values depending on the |
| 52 | +# parameter *where*. The number of *x* and *y* values is the same. |
| 53 | +# |
| 54 | +# In contrast, `.pyplot.stairs` defines the positions of the steps via their |
| 55 | +# bounds *edges*, which is one element longer than the step values. |
51 | 56 |
|
52 | 57 | bins = np.arange(14) |
53 | | -centers = bins[:-1] + np.diff(bins)/2 |
| 58 | +centers = bins[:-1] + np.diff(bins) / 2 |
54 | 59 | y = np.sin(centers / 2) |
55 | 60 |
|
56 | | -plt.step(bins[:-1], y, where='post', label='Step(where="post")') |
| 61 | +plt.step(bins[:-1], y, where='post', label='step(where="post")') |
57 | 62 | plt.plot(bins[:-1], y, 'o--', color='grey', alpha=0.3) |
58 | 63 |
|
59 | | -plt.stairs(y - 1, bins, baseline=None, label='Stairs') |
| 64 | +plt.stairs(y - 1, bins, baseline=None, label='stairs()') |
60 | 65 | plt.plot(centers, y - 1, 'o--', color='grey', alpha=0.3) |
61 | 66 | plt.plot(np.repeat(bins, 2), np.hstack([y[0], np.repeat(y, 2), y[-1]]) - 1, |
62 | 67 | 'o', color='red', alpha=0.2) |
63 | 68 |
|
64 | 69 | plt.legend() |
65 | | -plt.title('plt.step vs plt.stairs') |
| 70 | +plt.title('step() vs. stairs()') |
66 | 71 | plt.show() |
67 | 72 |
|
68 | 73 | ############################################################################# |
|
0 commit comments