Shading the union of areas greater than 2 lines

123 views Asked by At

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.

1

There are 1 answers

1
Joe Todd On BEST ANSWER

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.