1

I'd like to compute the area inside of a curve defined by two vectors a and b. For your reference the curve looks something like this (pyplot.plot(a,b)):

curve to compute area

I saw matplotlib has a fill functionality that let you fill the area enclosed by the curve:

enter image description here

I'm wondering, there's any way to obtain the area filled using that same function? It would be very useful as the other way I'm thinking of computing that area is through numerical integration, much more cumbersome.

Thank you for your time.

Alfonso Santiago
  • 531
  • 4
  • 20
  • Does this answer your question? [How do I calculate the area of a 2d polygon?](https://stackoverflow.com/questions/451426/how-do-i-calculate-the-area-of-a-2d-polygon) – Joe Jun 17 '20 at 11:49
  • You can use Shapely for that. Or polygon3, I think. – Joe Jun 17 '20 at 11:50
  • [Calculate area of polygon given (x,y) coordinates](https://stackoverflow.com/questions/24467972/calculate-area-of-polygon-given-x-y-coordinates) – JohanC Jun 17 '20 at 12:26
  • With shapely: [How to calculate area of polygon from list of points with python?](https://stackoverflow.com/questions/47657795/how-to-calculate-area-of-polygon-from-list-of-points-with-python) – JohanC Jun 17 '20 at 12:34

1 Answers1

1

If you really want to find the area that was filled by matplotlib.pyplot.fill(a, b), you can use its output as follows:

def computeArea(pos):
    x, y = (zip(*pos))
    return 0.5 * numpy.abs(numpy.dot(x, numpy.roll(y, 1)) - numpy.dot(y, numpy.roll(x, 1)))

# pyplot.fill(a, b) will return a list of matplotlib.patches.Polygon.
polygon = matplotlib.pyplot.fill(a, b)

# The area of the polygon can be computed as follows:
# (you could also sum the areas of all polygons in the list).
print(computeArea(polygon[0].xy))

This method is based on this answer, and it is not the most efficient one.

jordiplam
  • 66
  • 3