|
1 | 1 | """ |
2 | | -================ |
3 | | -Using span_where |
4 | | -================ |
| 2 | +======================================================================== |
| 3 | +Shade regions defined by a logical mask using fill_between or span_where |
| 4 | +======================================================================== |
5 | 5 |
|
6 | | -Illustrate some helper functions for shading regions where a logical |
7 | | -mask is True. |
8 | | -
|
9 | | -See `matplotlib.collections.BrokenBarHCollection.span_where`. |
| 6 | +Shade regions where a logical mask is True with `.Axes.fill_between` or with |
| 7 | +`matplotlib.collections.BrokenBarHCollection.span_where`. |
10 | 8 | """ |
11 | 9 |
|
12 | 10 | import numpy as np |
|
15 | 13 |
|
16 | 14 |
|
17 | 15 | t = np.arange(0.0, 2, 0.01) |
18 | | -s1 = np.sin(2*np.pi*t) |
19 | | -s2 = 1.2*np.sin(4*np.pi*t) |
20 | | - |
21 | | - |
22 | | -fig, ax = plt.subplots() |
23 | | -ax.set_title('using span_where') |
24 | | -ax.plot(t, s1, color='black') |
25 | | -ax.axhline(0, color='black', lw=2) |
| 16 | +s = np.sin(2*np.pi*t) |
26 | 17 |
|
27 | | -collection = collections.BrokenBarHCollection.span_where( |
28 | | - t, ymin=0, ymax=1, where=s1 > 0, facecolor='green', alpha=0.5) |
29 | | -ax.add_collection(collection) |
| 18 | +fig, axs = plt.subplots(2, sharex=True, sharey=True) |
| 19 | +for ax in axs: |
| 20 | + ax.plot(t, s, color='black') |
| 21 | + ax.axhline(0, color='black') |
30 | 22 |
|
31 | | -collection = collections.BrokenBarHCollection.span_where( |
32 | | - t, ymin=-1, ymax=0, where=s1 < 0, facecolor='red', alpha=0.5) |
33 | | -ax.add_collection(collection) |
| 23 | +axs[0].set_title('using fill_between') |
| 24 | +axs[0].fill_between(t, 1, where=s > 0, facecolor='green', alpha=.5) |
| 25 | +axs[0].fill_between(t, -1, where=s < 0, facecolor='red', alpha=.5) |
34 | 26 |
|
| 27 | +axs[1].set_title('using span_where') |
| 28 | +axs[1].add_collection(collections.BrokenBarHCollection.span_where( |
| 29 | + t, ymin=0, ymax=1, where=s > 0, facecolor='green', alpha=0.5)) |
| 30 | +axs[1].add_collection(collections.BrokenBarHCollection.span_where( |
| 31 | + t, ymin=-1, ymax=0, where=s < 0, facecolor='red', alpha=0.5)) |
35 | 32 |
|
36 | 33 | plt.show() |
37 | 34 |
|
|
43 | 40 | # The use of the following functions, methods, classes and modules is shown |
44 | 41 | # in this example: |
45 | 42 | # |
| 43 | +# - `matplotlib.axes.Axes.fill_between` |
46 | 44 | # - `matplotlib.collections.BrokenBarHCollection` |
47 | 45 | # - `matplotlib.collections.BrokenBarHCollection.span_where` |
48 | 46 | # - `matplotlib.axes.Axes.add_collection` |
49 | | -# - `matplotlib.axes.Axes.axhline` |
0 commit comments