-4

How do I fill in the space beneath curve "a" (the sloping up line) and above "b" (horizontal)? Thank you.

#Plot data
sns.set(font_scale = 1.5, style = 'white', rc=None)

fig, ax = plt.subplots(figsize = (15,10))

a = sensitivity.plot(y = 0.2, ax = ax, linestyle = '--', color = 'gray')
b = sensitivity.plot(y = 0.3, ax = ax, linestyle = '-.', color = 'gray')
c = sensitivity.plot(y = 0.4, ax = ax, linestyle = ':', color = 'gray')
d = ax.hlines(y=7.5, xmin=100, xmax=900, colors='black', linestyles='-', lw=2, label='Single Short Line')

Output:

enter image description here

vestland
  • 55,229
  • 37
  • 187
  • 305
Andrew
  • 73
  • 1
  • 9
  • 3
    Using [`ax.fill_between`](https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.axes.Axes.fill_between.html#matplotlib.axes.Axes.fill_between)? – DavidG Feb 03 '21 at 09:37

1 Answers1

1

As DavidG suggested, you can indeed use ax.fill_between. See this page for some examples. In addition to that page, I will show you a very simple example below:

import numpy as np
import matplotlib.pyplot as plt

# Generate some data
x  = np.linspace(0, 3, 25)
y1 = x**2 + 1   # Main data
y2 = 0.85 * y1  # Lower boundary
y3 = 1.15 * y1  # Upper boundary

# Create a figure and axes
fig, ax = plt.subplots()

# This creates the blue shaded area
ax.fill_between(x, y2, y3, alpha=0.3) 

# Here, we plot the solid line in the 'center'
ax.plot(x, y1, c='k') 

# Here, we plot the boundaries of the blue shaded area
ax.plot(x, y2, c='k', ls='--', alpha=0.3)  # Lower boundary
ax.plot(x, y3, c='k', ls='--', alpha=0.3)  # Upper boundary

plt.show()

This piece of code will produce the following result: Sample plot using ax.fill_between

DeX97
  • 637
  • 3
  • 12
  • I get this error since I'm using sensitivity plots: TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' – Andrew Feb 03 '21 at 14:45
  • I am not sure what could be causing the problem, but the error basically indicates that the function `isfinite` is not defined for the dtype of the data in your `numpy` array. Maybe you can have a look at either [this answer](https://stackoverflow.com/questions/40809503/python-numpy-typeerror-ufunc-isfinite-not-supported-for-the-input-types) or [this one](https://stackoverflow.com/questions/45432735/matplotlib-datetimes-and-typeerror-ufunc-isfinite-not-supported-for-the-inp/45433424). You probably need to convert the data you want to plot to a `float` (using `data=np.array(data,dtype=float)`) – DeX97 Feb 03 '21 at 17:20