# [pyngl-talk] contour path coordinates from Ngl.contour

Fearon, Dr. Matthew, Contractor, Code 7533 matthew.fearon.ctr at nrlmry.navy.mil
Wed Dec 4 12:06:51 MST 2019

```Hi,

I was wondering if anyone has experience extracting the line path/segment coordinates that define a 2D contour field? I am interested in capturing the coordinates of the closed contour paths and then defining them as polygons.  I have been successful in doing this with matplotlib, shapely, and collections python modules, but I'd prefer the plotting functionality and graphical look of pyngl. And in my experience, having matplotlib and pyngl installed together has always given me problems with dependencies (at least in the anaconda environment). Below is an example of capturing contour path coordinates for a single value of 250 for the 2D variable vflx. I was curious if it is possible to do something similar with Ngl.contour as I do with matplotlib.plt.contour ? I have not figured out a way to pull the x/y coordinates from Ngl.contour that define the contours.

Thanks for the help,

Matt

clevels=[250.]

cn = plt.contour(lon2d,lat2d,vflx, levels=clevels)   #is it possible to do this with Ngl.contour ?

#loop over the number of contour levels in cn

for i in cn.collections:

#loop over all contours per level

for contour_path in i.get_paths():

#each enumerate gives the coordinates that defines the contour exterior

#to_polygons() closes the contour

for ncpts,cpts in enumerate(contour_path.to_polygons()):

try:

x = cpts[:,0]

y = cpts[:,1]

except:

x=[]

y=[]

for i, (lon,lat) in enumerate(cpts):

x.append(lon)

y.append(lat)

#create shapely Polygon with x and y

poly = Polygon([(k[0], k[1]) for k in zip(x,y)])

-------------- next part --------------
An HTML attachment was scrubbed...