0

Say I have two lines, defined by the data

x1 = [0,1,2,3]
y1 = [3,5,4,6]
x2 = [1.5,2.5,3.5,4.5]
y2 = [1,3,2,4]

which make the plot

plt.figure(figsize=(10,10))
plt.plot(x1, y1)
plt.plot(x2, y2)
plt.show()

enter image description here

How can I fill between those two lines? I want the polygon made by connecting the endpoints of these lines, but plt.fill_between and plt.fill_betweenx don't work, as they are on both different x and y ranges.

Nikko Cleri
  • 185
  • 1
  • 1
  • 11
  • I think there is no built-in function for this. You can add one point to each line manually (i.e., x1.append(x2[-1]) and y1.append(y2[-1]) for first line, and similar with appending to the head for the second one) and then use fill_between. – dimnnv Dec 21 '22 at 19:16
  • Supposing you're working with lists (numpy arrays need to be concatenated in another way): `plt.fill(x1 + x2[::-1], y1 + y2[::-1], color='purple', alpha=0.3)` – JohanC Dec 21 '22 at 21:25

0 Answers0