|
1 | 1 | """
|
2 |
| -============ |
3 |
| -axhspan Demo |
4 |
| -============ |
| 2 | +================================= |
| 3 | +Drawing regions that span an Axes |
| 4 | +================================= |
5 | 5 |
|
6 |
| -Create lines or rectangles that span the Axes in either the horizontal or |
7 |
| -vertical direction, and lines than span the Axes with an arbitrary orientation. |
| 6 | +`~.Axes.axhspan` and `~.Axes.axvspan` draw rectangles that span the Axes in either |
| 7 | +the horizontal or vertical direction and are bounded in the other direction. They are |
| 8 | +often used to highlight data regions. |
8 | 9 | """
|
9 | 10 |
|
10 | 11 | import matplotlib.pyplot as plt
|
11 | 12 | import numpy as np
|
12 | 13 |
|
13 |
| -t = np.arange(-1, 2, .01) |
14 |
| -s = np.sin(2 * np.pi * t) |
15 |
| - |
16 |
| -fig, ax = plt.subplots() |
17 |
| - |
18 |
| -ax.plot(t, s) |
19 |
| -# Thick red horizontal line at y=0 that spans the xrange. |
20 |
| -ax.axhline(linewidth=8, color='#d62728') |
21 |
| -# Horizontal line at y=1 that spans the xrange. |
22 |
| -ax.axhline(y=1) |
23 |
| -# Vertical line at x=1 that spans the yrange. |
24 |
| -ax.axvline(x=1) |
25 |
| -# Thick blue vertical line at x=0 that spans the upper quadrant of the yrange. |
26 |
| -ax.axvline(x=0, ymin=0.75, linewidth=8, color='#1f77b4') |
27 |
| -# Default hline at y=.5 that spans the middle half of the Axes. |
28 |
| -ax.axhline(y=.5, xmin=0.25, xmax=0.75) |
29 |
| -# Infinite black line going through (0, 0) to (1, 1). |
30 |
| -ax.axline((0, 0), (1, 1), color='k') |
31 |
| -# 50%-gray rectangle spanning the Axes' width from y=0.25 to y=0.75. |
32 |
| -ax.axhspan(0.25, 0.75, facecolor='0.5') |
33 |
| -# Green rectangle spanning the Axes' height from x=1.25 to x=1.55. |
34 |
| -ax.axvspan(1.25, 1.55, facecolor='#2ca02c') |
| 14 | +fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(7, 3)) |
| 15 | + |
| 16 | +np.random.seed(19680801) |
| 17 | +s = 2.9 * np.convolve(np.random.randn(500), np.ones(30) / 30, mode='valid') |
| 18 | +ax1.plot(s) |
| 19 | +ax1.axhspan(-1, 1, alpha=0.1) |
| 20 | +ax1.set_ylim(-1.5, 1.5) |
| 21 | + |
| 22 | + |
| 23 | +mu = 8 |
| 24 | +sigma = 2 |
| 25 | +x = np.linspace(0, 16, 401) |
| 26 | +y = np.exp(-((x-mu)**2)/(2*sigma**2)) |
| 27 | +ax2.axvspan(mu-2*sigma, mu-sigma, color='0.95') |
| 28 | +ax2.axvspan(mu-sigma, mu+sigma, color='0.9') |
| 29 | +ax2.axvspan(mu+sigma, mu+2*sigma, color='0.95') |
| 30 | +ax2.axvline(mu, color='darkgrey', linestyle='--') |
| 31 | +ax2.plot(x, y) |
35 | 32 |
|
36 | 33 | plt.show()
|
| 34 | + |
| 35 | +# %% |
| 36 | +# |
| 37 | +# .. admonition:: References |
| 38 | +# |
| 39 | +# The use of the following functions, methods, classes and modules is shown |
| 40 | +# in this example: |
| 41 | +# |
| 42 | +# - `matplotlib.axes.Axes.axhspan` / `matplotlib.pyplot.axhspan` |
| 43 | +# - `matplotlib.axes.Axes.axvspan` / `matplotlib.pyplot.axvspan` |
| 44 | +# |
| 45 | +# |
| 46 | +# .. seealso:: |
| 47 | +# |
| 48 | +# `~.Axes.axhline`, `~.Axes.axvline`, `~.Axes.axline` draw infinite lines. |
0 commit comments