1

How could Python be used to find the area between the line y = x and the plotted points. Is there a well-known module with this functionality, or what is a simple solution?

import matplotlib.pyplot as plt
import numpy as np

x = ([[0.        ],[0.00862069],[0.03448276],[0.03448276],[0.06896552],
[0.0862069 ],[0.09482759],[0.09482759],[0.11206897],[0.11206897],
[0.15517241],[0.15517241],[0.1637931 ],[0.1637931 ],[0.20689655],
[0.20689655],[0.24137931],[0.25862069],[0.30172414],[0.3362069 ],
[0.37068966],[0.37068966],[0.45689655],[0.47413793],[0.5       ],
[0.53448276],[0.56034483],[0.67241379],[0.68965517],[0.75862069],
[0.77586207],[0.81034483],[0.82758621],[0.86206897],[0.87931034],
[0.9137931 ],[0.9137931 ],[0.93965517],[0.93965517],[0.95689655],
[0.96551724],[0.97413793],[0.97413793],[0.98275862],[0.98275862],
[0.99137931],[0.99137931],[1.        ],[1.        ]])

y = ([[0.        ],[0.        ],[0.        ],[0.26315789],[0.26315789],[0.26315789],
[0.26315789],[0.31578947],[0.31578947],[0.42105263],[0.42105263],[0.47368421],
[0.47368421],[0.52631579],[0.52631579],[0.57894737],[0.57894737],[0.57894737],
[0.57894737],[0.57894737],[0.57894737],[0.63157895],[0.63157895],[0.63157895],
[0.63157895],[0.63157895],[0.63157895],[0.63157895],[0.63157895],[0.63157895],
[0.63157895],[0.63157895],[0.63157895],[0.63157895],[0.63157895],[0.63157895],
[0.68421053],[0.68421053],[0.73684211],[0.73684211],[0.78947368],[0.78947368],
[0.84210526],[0.84210526],[0.89473684],[0.89473684],[0.94736842],[0.94736842],
[1.        ]])

plt.title('Find the Area between the line and the green connected points', fontsize=14)
plt.plot(x, y, 'green')
plt.legend(loc='lower right', fontsize=18)
plt.plot([0,1],[0,1])
plt.xlim([0,1])
plt.ylim([0,1.05])
plt.ylabel('y-axis', fontsize=16)
plt.xlabel('x-axis', fontsize=16)

Plot

blunova
  • 2,122
  • 3
  • 9
  • 21
John
  • 11
  • 6
  • Does this answer your question? [Find the area between two curves plotted in matplotlib (fill\_between area)](https://stackoverflow.com/questions/25439243/find-the-area-between-two-curves-plotted-in-matplotlib-fill-between-area) – blunova Jun 20 '22 at 21:51
  • That does answer the question! I just have to convert the arrays x and y into a single array using x_y_curve1 = np.column_stack((x, y)); the x_y_curve2 = [(0,0),(1,1)] represents the line y = x – John Jun 21 '22 at 16:29

1 Answers1

0

If you see your points as the vertices of a polygon, you can use the shoelace formula. Fastest way to Shoelace formula

CAUTION: the curve crosses x=y, so you have to specify if you want the algebraic area or the absolute area.

  • Thanks for providing an answer! I haven't tried it yet since I was able to adapt a suggested previous solution that blunova pointed out. It will be interesting to see if one solution is more convenient with the code that I was already using. – John Jun 21 '22 at 16:33