1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
"""
==========================================================
Shade regions defined by a logical mask using fill_between
==========================================================
"""
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2, 0.01)
s = np.sin(2*np.pi*t)
fig, ax = plt.subplots()
ax.plot(t, s, color='black')
ax.axhline(0, color='black')
ax.fill_between(t, 1, where=s > 0, facecolor='green', alpha=.5)
ax.fill_between(t, -1, where=s < 0, facecolor='red', alpha=.5)
plt.show()
# %%
#
# .. admonition:: References
#
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.axes.Axes.fill_between`
#
# .. tags::
#
# styling: conditional
# plot-style: fill_between
# level: beginner
|