0

I am plotting contours for various levels on an image with matplotlib. I'd like to compute the total area enclosed in all contours above a given contour level. Here's an example of the code I am starting with:

import matplotlib.pyplot as plt

ax = plt.subplot()
myimage = ax.imshow(im.T,interpolation='nearest',origin='lower',norm=LogNorm(),extent=ext)
mylevels=[1e-3,1e-2,1e-1]
cs = plt.contour(im.T,levels=mylevels,colors['white','yellow','red'],origin='lower')

The image im can be whatever 2d image here.

This will generate many contours, and what I am wondering is how to compute the total area enclosed within e.g. the first contour level. Any idea ?

Thanks.

Nuanda
  • 75
  • 7
  • https://stackoverflow.com/questions/22678990/how-can-i-calculate-the-area-within-a-contour-in-python-using-the-matplotlib – JohanC Jul 06 '21 at 23:54
  • Thanks Johan, I had seen this post but it seems to me that it works only for one single contour. If plt.contour() results in many contours (as it would be the case for an image simply built from a random field), this won't work I think. What I'm looking for is a general method to compute the total area enclosed by all contours (for a given level). Any further idea ? – Nuanda Jul 08 '21 at 08:59
  • Couldn't you just sum the areas of the separate contours? – JohanC Jul 08 '21 at 10:01
  • How do I access the separate contours one by one then ? – Nuanda Jul 08 '21 at 15:56

0 Answers0