BaRud
BaRud

Reputation: 3218

fill the common area under both curve

How I can fill the common area under both the curve?

import matplotlib.pyplot as plt
import numpy as np
import scipy.special as sp

x = np.linspace(-4, 4, num=1000)
r = abs(x)

zeta = 1.0

psi_STO = (zeta**3 / np.pi)**(0.5) * np.exp(-zeta * r)
plt.figure(figsize=(4, 3))
plt.plot(x, psi_STO, color="C0")
plt.plot(x + 3, psi_STO, color="C0")
plt.show()

If I use:

plt.fill_betweenx(psi_STO, -1, 4, color="C1")

I am getting a plot as:

enter image description here

Upvotes: 0

Views: 56

Answers (2)

Julien
Julien

Reputation: 15071

This?

dx = 3 # x shift
di = int(dx/(x[1]-x[0])) # index shift
plt.fill_between(x[di:], np.minimum(psi_STO[:-di], psi_STO[di:]))

Upvotes: 1

mozway
mozway

Reputation: 260290

You can use fill_between. As your Xs are not aligned, you need to make a bit of calculations first to find the common range. This will depend on the number of points in the linspace. Here, I computed it manually: as the shift is of 3, there are 375 points difference (250 per unit).

import matplotlib.pyplot as plt
import numpy as np
import scipy.special as sp

x = np.linspace(-4, 4, num=1000)
r = abs(x)

zeta = 1.0

psi_STO = (zeta**3 / np.pi)**(0.5) * np.exp(-zeta * r)
plt.figure(figsize=(4, 3))
plt.plot(x, psi_STO, color="C0")
plt.plot(x + 3, psi_STO, color="C0")

x_common = (x+3)[:-375]
min_common = np.min([psi_STO[:-375], psi_STO[375:]], axis=0)
plt.plot(x_common, min_common, color='r')

plt.fill_between(x_common,min_common, color="#FFB0B0")
plt.show()

output: matplotlib fill_between

Upvotes: 1

Related Questions