0

I'm attempting to fill in an area enclosed by four curves but can't seem to get the fill I want: I'm using fill_between() but that can only handle two lines at most. Here's the code I currently have:

fig,ax = plt.subplots()
x = np.arange(start = -80,stop = 80,step = 1e-3)
yArc = np.sqrt(80**2 - x**2) 
yLL = -2*x - 10
yRL = 2*x - 10
yTop = 0*x + 150
ax.fill_between(x,yArc,yTop,where = yArc<yTop,color = 'red',alpha = 0.8)
plt.show()

Here's the output: Region of Interest

Note: In case you can't see, there is a horizontal line just about where the free-throw line is.

PM25
  • 33
  • 5

2 Answers2

0

You can use set operations between different filters to fill the central part first,

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x = np.arange(start=-80, stop=80, step=1e-3)

yArc = np.sqrt(80 ** 2 - x ** 2)
yLL = -2 * x - 10
yRL = 2 * x - 10
yTop = 0 * x + 150

plt.plot(x, yArc, "r")
plt.plot(x, yLL, "b")
plt.plot(x, yRL, "g")
plt.plot(x, yTop, "k")

filt_1 = yArc < yTop
filt_2 = yArc > yLL
filt_3 = yArc > yRL
filt = filt_1 & filt_2 & filt_3

ax.fill_between(x, yArc, yTop, where=filt, color="orange", alpha=0.8)

plt.show()

Producing:

enter image description here

And finally fill the two remaining right triangles, see for example: matplotlib: use fill_between to make coloured triangles

Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53
0

The most obvious way to tackle this, is to create a bottom curve as the maximum of the three curves:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x = np.arange(start=-80, stop=80, step=1e-3)

yArc = np.sqrt(80 ** 2 - x ** 2)
yLL = -2 * x - 10
yRL = 2 * x - 10
yTop = 0 * x + 150

plt.plot(x, yArc, "r")
plt.plot(x, yLL, "b")
plt.plot(x, yRL, "g")
plt.plot(x, yTop, "k")

yBottom = np.max([yArc, yLL, yRL], axis=0)
ax.fill_between(x, yBottom, yTop, where=yBottom <= yTop, color="orange", alpha=0.8)

resulting plot

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Is there any reason you prefer a more complicated solution that leaves out the triangle areas? – JohanC Jun 14 '20 at 00:03