0

Could you please help me shade the area highlighted as red below.

enter image description here

Everything I have tried or read on this topic using "fill_between" will fill the area between the lines. However, this actually needs to shade the area greater than Y=X UNION'd with the area great than 1/X (which is shaded as red in my crude example.

As you can see, my attempts always result in some combination of the area between the lines being filled.

Code:

x = np.linspace(0.0,15.0,150)

y = x
y_ = 1/x

d = scipy.zeros(len(y))

fig, ax = plt.subplots(1,1)
ax.plot(x, y)
ax.plot(x, y_)
ax.legend(["y >= x", "y >= 1/x"])

ax.fill_between(x, y, y_, where=y_>d, alpha=0.5, interpolate=True)

Thank you for the suggestions

Regards, F.

Fraf
  • 13
  • 2
  • 1
    Unrelated to the actual question: `DeprecationWarning: scipy.zeros is deprecated and will be removed in SciPy 2.0.0, use numpy.zeros instead d = scipy.zeros(len(y))` – Mr. T Nov 28 '20 at 18:54

1 Answers1

1

What about this?

import numpy as np
import matplotlib.pyplot as plt


x = np.linspace(0.0,15.0,150)

y = x
y_ = 1/x

ceiling = 100.0
max_y = np.maximum(y, y_)

d = np.zeros(len(y))

fig, ax = plt.subplots(1,1)
ax.plot(x, y)
ax.plot(x, y_)
ax.legend(["y >= x", "y >= 1/x"])

plt.ylim((0, 10))
ax.fill_between(x, max_y, ceiling, where=y_>d, alpha=0.5, interpolate=True)

plt.show()

i.e. take the max (np.maximum) of the two functions, then fill the area between this new max function and some suitably high ceiling.

Of course you also have to manually set the y-lim or your plot will reach the ceiling value in y.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Joe Todd
  • 814
  • 6
  • 13
  • 1
    Thank you so much, this is perfect, and exactly captures what I was trying to do. Thanks again! – Fraf Nov 28 '20 at 18:59