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 span
nums = 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 span
nums = 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 and 0.5. Just like any of the other plotting commands that involve areas, we can give this hatches, change the color of it, and add any of the appearance attributes as well, as shown in the following code:

# Adding a horizontal and vertical span
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axvspan(-0.5,0.5, color='k', alpha=0.5)

The output can be seen as follows:

We can also set how far the vertical line goes across the axis. By default, the vertical line covers up the entire axis, as follows:

# Adding a horizontal and vertical span
nums = 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:

Now, if we pass a third keyword argument, xmin, we can give a minimum value, and xmax as the maximum value. Importantly, these are in the coordinates of the axis, so they go from 0 to 1, as shown in the following code:

# Span Axis coverage (min/max)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhspan(-0.5,0.5, xmin=0, xmax=1, alpha=0.5)

From the preceding code, we get the following output::

Note that in the data dimensions, the coordinates go from 0 to 10. But as usual, the axes always span from 0 to 1 in a particular coordinate system.

Also, by changing xmin to 0.5, we get the following:

# Span Axis coverage (min/max)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhspan(-0.5,0.5, xmin=0.5, xmax=1, alpha=0.5)

We will get the following output:

Also, by changing xmax to 0.75, we get:

# Span Axis coverage (min/max)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhspan(-0.5,0.5, xmin=0.5, xmax=0.75, alpha=0.5)

We will get the following output:

This is a quick and easy way to highlight particular points of your data that you want to show to the viewer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.55.106