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

Fearon, Dr. Matthew, Contractor, Code 7533 matthew.fearon.ctr at nrlmry.navy.mil
Wed Dec 23 16:07:43 MST 2020

```Hi,

I sent this question to pyngl-talk a while back, please see below. Thought
I'd try again. If anyone has any advice that would be great.

Thank you,

Matt

From: pyngl-talk <pyngl-talk-bounces at ucar.edu> On Behalf Of Fearon, Dr.
Matthew, Contractor, Code 7533 via pyngl-talk
Sent: Wednesday, December 4, 2019 11:07 AM
To: 'pyngl-talk at ucar.edu' <pyngl-talk at ucar.edu>
Subject: [pyngl-talk] contour path coordinates from Ngl.contour

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 or open 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...
URL: <https://mailman.ucar.edu/pipermail/pyngl-talk/attachments/20201223/31ca74f3/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 6659 bytes
Desc: not available
URL: <https://mailman.ucar.edu/pipermail/pyngl-talk/attachments/20201223/31ca74f3/attachment.p7s>
```