Shade regions defined by a logical mask using fill_between#

import numpy as np
import matplotlib.pyplot as plt


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()
span regions

References

The use of the following functions, methods, classes and modules is shown in this example:

Gallery generated by Sphinx-Gallery