-1

I have in a list of points two different curves (y1, y2) and I want to find the area between the curves when:

  • y1 > y2
  • y1 < y2

I have found this post, but it only calculates the sum of both areas.

If we plot what I want, I want separately the blue area and the red area.

separately

Ricardohs
  • 43
  • 1
  • 8

1 Answers1

2

Edit: I noticed in hindsight, that this solution is not exact and there are probably cases where it doesn't work at all. As long as there is no other better answer I will leave this here.

You can use

diff = y1 - y2 # calculate difference
posPart = np.maximum(diff, 0) # only keep positive part, set other values to zero
negPart = -np.minimum(diff, 0) # only keep negative part, set other values to zero

to seperate the blue from the red part. Then calculate their areas with np.trapz:

posArea = np.trapz(posPart)
negArea = np.trapz(negPart)
markuscosinus
  • 2,248
  • 1
  • 8
  • 19
  • Thanks, I have plot it the areas and look very promising. Otherwise, the areas does not exactly match with the area of the post linked. Do you know why this could be? – Ricardohs Mar 15 '19 at 12:58
  • I just noticed, that this solution only works as an approximation, so the values are not exactly right. – markuscosinus Mar 17 '19 at 12:08