Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport PR #24843 on branch v3.6.x (Show that fill_between and span_where provide similar functionalities.) #24852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions examples/lines_bars_and_markers/span_regions.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()

Expand All @@ -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`