Re: [Matplotlib-users] Extract the x- and y-locations from the contour matrix C

Hi Nils,

Extracting the x and y locations from a contour is a bit of a pain in matplotlib (or at least was last time I tried it). if you do something like

c = ax.contour(x,y,z,…)

(where … stands in for the arguments you might want to supply)

then c is a QuadContourSet. To get at the contour data you need to do something like

p = c.collections[i].get_paths()

(if you want the i’th collection, i.e. contour level number i, or you could iterate of c.collections)

then p is a list of Path objects corresponding to each separate contour.

p.vertices is then an N x 2 array of the x and y values

An alternative to this is to use scikit-image package, in particular skimage.measure.find_contours. However in that case you do something like:

c = find_contours(image,level)

(where image is a 2D array) and you get back a list of (n,2) arrays with the indices of the image for the contour. To get the x,y values then you need to scale those indices accordingly.

Jon

···

Jonathan D. Slavin

Astrophysicist - High Energy Astrophysics Division

Center for Astrophysics | Harvard & Smithsonian

Office: (617) 496-7981 | Cell: (781) 363-0035
60 Garden Street | MS 83 | Cambridge, MA 02138