|
| 1 | +""" |
| 2 | +=========== |
| 3 | +Stairs Demo |
| 4 | +=========== |
| 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. |
| 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. |
| 13 | +""" |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +import matplotlib.pyplot as plt |
| 17 | +from matplotlib.patches import StepPatch |
| 18 | + |
| 19 | +np.random.seed(0) |
| 20 | +h, bins = np.histogram(np.random.normal(5, 3, 5000), |
| 21 | + bins=np.linspace(0, 10, 20)) |
| 22 | + |
| 23 | +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') |
| 27 | +axs[0].set_title("Step Histograms") |
| 28 | + |
| 29 | +axs[1].stairs(np.arange(1, 6, 1), fill=True, |
| 30 | + label='Filled histogram\nw/ automatic edges') |
| 31 | +axs[1].stairs(np.arange(1, 6, 1)*0.3, np.arange(2, 8, 1), |
| 32 | + orientation='horizontal', hatch='//', |
| 33 | + label='Hatched histogram\nw/ horizontal orientation') |
| 34 | +axs[1].set_title("Filled histogram") |
| 35 | + |
| 36 | +patch = StepPatch(values=[1, 2, 3, 2, 1], |
| 37 | + edges=range(1, 7), |
| 38 | + label=('Patch derived underlying object\n' |
| 39 | + 'with default edge/facecolor behaviour')) |
| 40 | +axs[2].add_patch(patch) |
| 41 | +axs[2].set_xlim(0, 7) |
| 42 | +axs[2].set_ylim(-1, 5) |
| 43 | +axs[2].set_title("StepPatch artist") |
| 44 | + |
| 45 | +for ax in axs: |
| 46 | + ax.legend() |
| 47 | +plt.show() |
| 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() |
| 67 | + |
| 68 | +############################################################################# |
| 69 | +# |
| 70 | +# ------------ |
| 71 | +# |
| 72 | +# References |
| 73 | +# """""""""" |
| 74 | +# |
| 75 | +# The use of the following functions, methods, classes and modules is shown |
| 76 | +# in this example: |
| 77 | + |
| 78 | +import matplotlib |
| 79 | +matplotlib.axes.Axes.stairs |
| 80 | +matplotlib.pyplot.stairs |
| 81 | +matplotlib.patches.StepPatch |
0 commit comments