diff --git a/examples/lines_bars_and_markers/span_regions.py b/examples/lines_bars_and_markers/span_regions.py index 79ebfcd008f2..9a5e8e3428d1 100644 --- a/examples/lines_bars_and_markers/span_regions.py +++ b/examples/lines_bars_and_markers/span_regions.py @@ -1,12 +1,10 @@ """ -================ -Using span_where -================ +======================================================================== +Shade regions defined by a logical mask using fill_between or span_where +======================================================================== -Illustrate some helper functions for shading regions where a logical -mask is True. - -See `matplotlib.collections.BrokenBarHCollection.span_where`. +Shade regions where a logical mask is True with `.Axes.fill_between` or with +`matplotlib.collections.BrokenBarHCollection.span_where`. """ import numpy as np @@ -15,23 +13,22 @@ t = np.arange(0.0, 2, 0.01) -s1 = np.sin(2*np.pi*t) -s2 = 1.2*np.sin(4*np.pi*t) - - -fig, ax = plt.subplots() -ax.set_title('using span_where') -ax.plot(t, s1, color='black') -ax.axhline(0, color='black', lw=2) +s = np.sin(2*np.pi*t) -collection = collections.BrokenBarHCollection.span_where( - t, ymin=0, ymax=1, where=s1 > 0, facecolor='green', alpha=0.5) -ax.add_collection(collection) +fig, axs = plt.subplots(2, sharex=True, sharey=True) +for ax in axs: + ax.plot(t, s, color='black') + ax.axhline(0, color='black') -collection = collections.BrokenBarHCollection.span_where( - t, ymin=-1, ymax=0, where=s1 < 0, facecolor='red', alpha=0.5) -ax.add_collection(collection) +axs[0].set_title('using fill_between') +axs[0].fill_between(t, 1, where=s > 0, facecolor='green', alpha=.5) +axs[0].fill_between(t, -1, where=s < 0, facecolor='red', alpha=.5) +axs[1].set_title('using span_where') +axs[1].add_collection(collections.BrokenBarHCollection.span_where( + t, ymin=0, ymax=1, where=s > 0, facecolor='green', alpha=0.5)) +axs[1].add_collection(collections.BrokenBarHCollection.span_where( + t, ymin=-1, ymax=0, where=s < 0, facecolor='red', alpha=0.5)) plt.show() @@ -43,7 +40,7 @@ # The use of the following functions, methods, classes and modules is shown # in this example: # +# - `matplotlib.axes.Axes.fill_between` # - `matplotlib.collections.BrokenBarHCollection` # - `matplotlib.collections.BrokenBarHCollection.span_where` # - `matplotlib.axes.Axes.add_collection` -# - `matplotlib.axes.Axes.axhline`