Skip to content

Commit 087ca0c

Browse files
authored
Merge pull request matplotlib#24843 from anntzer/fbsw
Show that fill_between and span_where provide similar functionalities.
2 parents aa846a3 + 36f63f8 commit 087ca0c

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
"""
2-
================
3-
Using span_where
4-
================
2+
========================================================================
3+
Shade regions defined by a logical mask using fill_between or span_where
4+
========================================================================
55
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`.
108
"""
119

1210
import numpy as np
@@ -15,23 +13,22 @@
1513

1614

1715
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)
2617

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')
3022

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)
3426

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))
3532

3633
plt.show()
3734

@@ -43,7 +40,7 @@
4340
# The use of the following functions, methods, classes and modules is shown
4441
# in this example:
4542
#
43+
# - `matplotlib.axes.Axes.fill_between`
4644
# - `matplotlib.collections.BrokenBarHCollection`
4745
# - `matplotlib.collections.BrokenBarHCollection.span_where`
4846
# - `matplotlib.axes.Axes.add_collection`
49-
# - `matplotlib.axes.Axes.axhline`

0 commit comments

Comments
 (0)