Adding spans that cover whole regions

To color the whole region, say between 0.5 and -0.5, insert the horizontal span as follows:

# Adding a horizontal and vertical spannums = np.arange(0,10,0.1)plt.plot(nums, np.sin(nums))plt.axhspan(-0.5,0.5)

We get the output here as follows:

But the shaded region blocks out a lot of our data. Hence, we will make this black and give it an alpha of 0.5, as follows:

# Adding a horizontal and vertical spannums = np.arange(0,10,0.1)plt.plot(nums, np.sin(nums))plt.axhspan(-0.5,0.5, alpha=0.5)

We will get the following output:

Performing the same thing for a vertical span will give you a region between -0.5 ...

Get Mastering Matplotlib 2.x now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.