Skip to content Skip to sidebar Skip to footer

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. Using the following code. plt.scatter(z1,y1, s = 0.5, color = 'blue') plt.scatter(z2,

Solution 1:

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

Solution 2:

Try this code:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2 * np.pi * x)
y2 = 1.2 * np.sin(4 * np.pi * x)


fig, (ax1) = plt.subplots(1, sharex=True)
ax1.fill_between(x, 0, y1)
ax1.set_ylabel('between y1 and 0')

Post a Comment for "Fill Area Between Two Curves In Python"