Finding the center of contour levels (or understanding collections.get_verts())

I need to find the centers of contour levels. My current approach is to create a contour set:

aContourSet = pylab.contourf(xi, yi, zi, 16, cmap = pylab.cm.jet)

and then for each collection in the collections, extract the vertices and then find the center(s) of the polygon(s). For example:

for aCollection in aContourSet.collections:
     aListOfPolygons = decompose(aCollection.get_verts(),
                                 eps = eps,
                                 verbose = verbose)

Where decompose walks through the vertices list and extracts the polygons by finding all of the closed polygons in the collection. This algorithm starts at the beginning of the list of vertices and looks for the same point. If that point is found, the algorithm calls this a closed polygon and then starts the search over at the next point in the list. If no match is found, that point is assumed to be the only point in the polygon and the search continues at the next point. This process is continued until the list is exhausted. (See attached file for implementation details of decompose and the code for finding the center of a polygon.).

However when I visually examine the results, some of the contour centers seem to be incorrect. I know that for some polygons that the center is not found inside the polygon. My problem seems to be with the contour levels that have multiple polygons. Hence, I think that my code to extract the closed polygons is it fault. Is there a definition of how the contouring routine populates the collection? Or is there a easier way to find the centers of the contours?

Thanks,

Jody Winston

decompose.py (3.28 KB)