Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill area between two curves in python

I am trying to shade the area between two curves that I have plotted. This is what I plotted. enter image description here

Using the following code.

plt.scatter(z1,y1, s = 0.5, color = 'blue')
plt.scatter(z2,y2, s = 0.5, color = 'orange')

I tried using plt.fill_between() but for this to work I need to have the same data on the x_axis (would need to do something like plt.fill_between(x,y1,y2)). Is there any other function that might help with this or am I just using fill_between wrong.

like image 545
Susan-l3p Avatar asked Jun 24 '26 07:06

Susan-l3p


1 Answers

You can try with:

plt.fill(np.append(z1, z2[::-1]), np.append(y1, y2[::-1]), 'lightgrey')

For example:

import numpy as np
import matplotlib.pyplot as plt

x1 = np.array([1,2,3])
y1 = np.array([2,3,4])
x2 = np.array([2,3,4,5,6])
y2 = np.array([1,2,3,4,5])
# plt.plot(x1, y1, 'o')
# plt.plot(x2, y2, 'x')

plt.scatter(x1, y1, s = 0.5, color = 'blue')
plt.scatter(x2, y2, s = 0.5, color = 'orange')
plt.fill(np.append(x1, x2[::-1]), np.append(y1, y2[::-1]), 'lightgrey')
plt.show()

enter image description here

like image 73
Joe Avatar answered Jun 26 '26 19:06

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!