Retrieve Coordinates from Contour

Thanks for the quick reply.

I may not have been totally clear on
what I’m trying to save (or I totally misunderstood what you were
trying to say - which is certainly possible).

I’m not wanting to
save the lat, lon pairs from the map projection. I’m trying to save
the lat, lon pairs of the contour. Using my original example, if I’m
plotting winds every 5 m/s, I’m wanting to pull off the lat, lon pairs
for the 5 m/s contours.

I’ll check out the website and see if I find anything there.

-Patrick

···

On Sat, Aug 2, 2008 at 6:13 PM, Jeff Whitaker <jswhit@…146…> wrote:

Patrick Marsh wrote:

Hi Everyone,

First email here…

I am plotting meteorological data using matplotlib and basemap - and can do this just fine. However, I would like to backout the coordinates being used for the contours that are plotted.

For example, if I were to contour windspeed every 5 m/s and plot this (which I can do just fine), I would like to save a copy of the lat, lon pairs as a text file. However, I cannot for the life of me figure out how to do this. I have a feeling it is pretty simple and I’m just over looking something that can do this. Any help would be appreciated.

Thanks,

-Patrick Marsh

Graduate Student

School of Meteorology

University of Oklahoma

Patrick: contour and contourf return a ContourSet object. ContourSet.collections is a matplotlib LineCollection (for contour) or a PolyCollection (for contourf). You can retrieve the x,y (map projection) coordinates from these, and transfer them back to lat/lon coordinates using the Basemap instance via

lons,lats = map(x,y,inverse=True) # map is a Basemap instance.

See http://matplotlib.sourceforge.net/doc/html/api/collections_api.html for more info on matplotlib collection objects.

HTH,

-Jeff



This SF.Net email is sponsored by the Moblin Your Move Developer’s challenge

Build the coolest Linux based applications with Moblin SDK & win great prizes

Grand prize is a trip for two to an Open Source event anywhere in the world

http://moblin-contest.org/redirect.php?banner_id=100&url=/



Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Jeffrey S. Whitaker Phone : (303)497-6313

NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449

325 Broadway Boulder, CO, USA 80305-3328

Patrick Marsh wrote:

Thanks for the quick reply.

I may not have been totally clear on what I'm trying to save (or I totally misunderstood what you were trying to say - which is certainly possible).

I'm not wanting to save the lat, lon pairs from the map projection. I'm trying to save the lat, lon pairs of the contour. Using my original example, if I'm plotting winds every 5 m/s, I'm wanting to pull off the lat, lon pairs for the 5 m/s contours.

Patrick: I know - that's what I was trying to explain. You can get the x,y coordinates of the *contours* from the collections attribute of the ContourSet object returned by contour. Then you convert those x,y values back to lon,lat values using the Basemap instance.

-Jeff

···

I'll check out the website and see if I find anything there.

-Patrick

    On Sat, Aug 2, 2008 at 6:13 PM, Jeff Whitaker <jswhit@…146… > <mailto:jswhit@…146…>> wrote:

        Patrick Marsh wrote:

            Hi Everyone,

            First email here...

            I am plotting meteorological data using matplotlib and
            basemap - and can do this just fine. However, I would
            like to backout the coordinates being used for the
            contours that are plotted.

            For example, if I were to contour windspeed every 5 m/s
            and plot this (which I can do just fine), I would like to
            save a copy of the lat, lon pairs as a text file.
             However, I cannot for the life of me figure out how to do
            this. I have a feeling it is pretty simple and I'm just
            over looking something that can do this. Any help would
            be appreciated.

            Thanks,

            -Patrick Marsh
            Graduate Student
            School of Meteorology
            University of Oklahoma

        Patrick: contour and contourf return a ContourSet object.
        ContourSet.collections is a matplotlib LineCollection (for
        contour) or a PolyCollection (for contourf). You can retrieve
        the x,y (map projection) coordinates from these, and transfer
        them back to lat/lon coordinates using the Basemap instance via

        lons,lats = map(x,y,inverse=True) # map is a Basemap instance.

        See
        http://matplotlib.sourceforge.net/doc/html/api/collections_api.html
        for more info on matplotlib collection objects.

        HTH,
        -Jeff

            ------------------------------------------------------------------------

            -------------------------------------------------------------------------
            This SF.Net email is sponsored by the Moblin Your Move
            Developer's challenge
            Build the coolest Linux based applications with Moblin SDK
            & win great prizes
            Grand prize is a trip for two to an Open Source event
            anywhere in the world
            http://moblin-contest.org/redirect.php?banner_id=100&url=/
            <http://moblin-contest.org/redirect.php?banner_id=100&url=/>
            ------------------------------------------------------------------------

            _______________________________________________
            Matplotlib-users mailing list
            Matplotlib-users@lists.sourceforge.net
            <mailto:Matplotlib-users@lists.sourceforge.net>
            https://lists.sourceforge.net/lists/listinfo/matplotlib-users
             
        -- Jeffrey S. Whitaker Phone : (303)497-6313
        NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449
        325 Broadway Boulder, CO, USA 80305-3328

--
Jeffrey S. Whitaker Phone : (303)497-6313
NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449
325 Broadway Boulder, CO, USA 80305-3328

Hi Jeff (and others):

Sorry for the misunderstanding. After your second email the first makes more sense.

However, I still cannot figure out how to extract the lat,lon pairs from the LineCollection object. Searching on the web and based on Scott’s suggestion from another email, I see that in the past you could use a get_vertex() option. However it appears to have been discontinued in mpl v 0.98.1.

I’m a relative newcomer to Python (less than 6 months) and even greener when it comes to the inner workings of matplotlib. I’m sure I’m going to kick myself when this is figured out…

-Patrick

···

On Sat, Aug 2, 2008 at 7:33 PM, Jeff Whitaker <jswhit@…146…> wrote:

Patrick Marsh wrote:

Thanks for the quick reply.

I may not have been totally clear on what I’m trying to save (or I totally misunderstood what you were trying to say - which is certainly possible).

I’m not wanting to save the lat, lon pairs from the map projection. I’m trying to save the lat, lon pairs of the contour. Using my original example, if I’m plotting winds every 5 m/s, I’m wanting to pull off the lat, lon pairs for the 5 m/s contours.

Patrick: I know - that’s what I was trying to explain. You can get the x,y coordinates of the contours from the collections attribute of the ContourSet object returned by contour. Then you convert those x,y values back to lon,lat values using the Basemap instance.

-Jeff

I’ll check out the website and see if I find anything there.

-Patrick

On Sat, Aug 2, 2008 at 6:13 PM, Jeff Whitaker <jswhit@...146... > > <mailto:jswhit@...146...>> wrote:



    Patrick Marsh wrote:



        Hi Everyone,



        First email here...



        I am plotting meteorological data using matplotlib and

        basemap - and can do this just fine.  However, I would

        like to backout the coordinates being used for the

        contours that are plotted.



        For example, if I were to contour windspeed every 5 m/s

        and plot this (which I can do just fine), I would like to

        save a copy of the lat, lon pairs as a text file.

         However, I cannot for the life of me figure out how to do

        this.  I have a feeling it is pretty simple and I'm just

        over looking something that can do this.  Any help would

        be appreciated.



        Thanks,



        -Patrick Marsh

        Graduate Student

        School of Meteorology

        University of Oklahoma





    Patrick:  contour and contourf return a ContourSet object.

    ContourSet.collections is a matplotlib LineCollection (for

    contour) or a PolyCollection (for contourf).  You can retrieve

    the x,y (map projection) coordinates from these, and transfer

    them back to lat/lon coordinates using the Basemap instance via



    lons,lats = map(x,y,inverse=True) # map is a Basemap instance.



    See

    [http://matplotlib.sourceforge.net/doc/html/api/collections_api.html](http://matplotlib.sourceforge.net/doc/html/api/collections_api.html)

    for more info on matplotlib collection objects.



    HTH,

    -Jeff



        ------------------------------------------------------------------------



        -------------------------------------------------------------------------

        This SF.Net email is sponsored by the Moblin Your Move

        Developer's challenge

        Build the coolest Linux based applications with Moblin SDK

        & win great prizes

        Grand prize is a trip for two to an Open Source event

        anywhere in the world

        [http://moblin-contest.org/redirect.php?banner_id=100&url=/](http://moblin-contest.org/redirect.php?banner_id=100&url=/)

        <[http://moblin-contest.org/redirect.php?banner_id=100&url=/](http://moblin-contest.org/redirect.php?banner_id=100&url=/)>

        ------------------------------------------------------------------------



        _______________________________________________

        Matplotlib-users mailing list

        Matplotlib-users@lists.sourceforge.net

mailto:Matplotlib-users@lists.sourceforge.net

        [https://lists.sourceforge.net/lists/listinfo/matplotlib-users](https://lists.sourceforge.net/lists/listinfo/matplotlib-users)

        





    --        Jeffrey S. Whitaker         Phone : (303)497-6313

    NOAA/OAR/CDC  R/PSD1        FAX   : (303)497-6449

    325 Broadway                Boulder, CO, USA 80305-3328

Jeffrey S. Whitaker Phone : (303)497-6313

NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449

325 Broadway Boulder, CO, USA 80305-3328

Patrick Marsh wrote:

Hi Jeff (and others):

Sorry for the misunderstanding. After your second email the first makes more sense.

However, I still cannot figure out how to extract the lat,lon pairs from the LineCollection object. Searching on the web and based on Scott's suggestion from another email, I see that in the past you could use a get_vertex() option. However it appears to have been discontinued in mpl v 0.98.1. <http://0.98.1.>

Scott: Sorry, you're right - it's far from obvious how to get the x,y coordinates pairs out of a LineCollection. Each contour has a 'collections' attribute that is a LineCollections object. The get_paths() method of the LineCollections object returns a list of a matplotlib.path.Path objects, each of which has a 'vertices' attribute.

So, something like this works for me in 0.98:

for xy in CS.collections[0].get_paths(): # get the paths for the first contour
    for xy in xy.vertices: # iterate over the Path objects
        # xy[0],xy[1] are the x,y coordinates
        # these are the lon,lat coords (map is the Basemap instance)
        lon, lat = map(xy[0],xy[1],inverse=True)
       

I'm a relative newcomer to Python (less than 6 months) and even greener when it comes to the inner workings of matplotlib. I'm sure I'm going to kick myself when this is figured out...

-Patrick

No worries - don't kick yourself. Although matplotlib is quite easy to use in general, the 'inner workings' take a while to grok.

-Jeff

···

On Sat, Aug 2, 2008 at 7:33 PM, Jeff Whitaker <jswhit@…146… > <mailto:jswhit@…146…>> wrote:

    Patrick Marsh wrote:

        Thanks for the quick reply.

        I may not have been totally clear on what I'm trying to save
        (or I totally misunderstood what you were trying to say -
        which is certainly possible).

        I'm not wanting to save the lat, lon pairs from the map
        projection. I'm trying to save the lat, lon pairs of the
        contour. Using my original example, if I'm plotting winds
        every 5 m/s, I'm wanting to pull off the lat, lon pairs for
        the 5 m/s contours.

    Patrick: I know - that's what I was trying to explain. You can
    get the x,y coordinates of the *contours* from the collections
    attribute of the ContourSet object returned by contour. Then you
    convert those x,y values back to lon,lat values using the Basemap
    instance.

    -Jeff

        I'll check out the website and see if I find anything there.

        -Patrick

           On Sat, Aug 2, 2008 at 6:13 PM, Jeff Whitaker > <jswhit@…146… <mailto:jswhit@…146…> > <mailto:jswhit@…146…>> wrote:

               Patrick Marsh wrote:

                   Hi Everyone,

                   First email here...

                   I am plotting meteorological data using matplotlib and
                   basemap - and can do this just fine. However, I would
                   like to backout the coordinates being used for the
                   contours that are plotted.

                   For example, if I were to contour windspeed every 5 m/s
                   and plot this (which I can do just fine), I would
        like to
                   save a copy of the lat, lon pairs as a text file.
                    However, I cannot for the life of me figure out
        how to do
                   this. I have a feeling it is pretty simple and I'm
        just
                   over looking something that can do this. Any help
        would
                   be appreciated.

                   Thanks,

                   -Patrick Marsh
                   Graduate Student
                   School of Meteorology
                   University of Oklahoma

               Patrick: contour and contourf return a ContourSet object.
               ContourSet.collections is a matplotlib LineCollection (for
               contour) or a PolyCollection (for contourf). You can
        retrieve
               the x,y (map projection) coordinates from these, and
        transfer
               them back to lat/lon coordinates using the Basemap
        instance via

               lons,lats = map(x,y,inverse=True) # map is a Basemap
        instance.

               See
                      http://matplotlib.sourceforge.net/doc/html/api/collections_api.html
               for more info on matplotlib collection objects.

               HTH,
               -Jeff

                          ------------------------------------------------------------------------

                          -------------------------------------------------------------------------
                   This SF.Net email is sponsored by the Moblin Your Move
                   Developer's challenge
                   Build the coolest Linux based applications with
        Moblin SDK
                   & win great prizes
                   Grand prize is a trip for two to an Open Source event
                   anywhere in the world
                          http://moblin-contest.org/redirect.php?banner_id=100&url=/
        <http://moblin-contest.org/redirect.php?banner_id=100&url=/>
                          <http://moblin-contest.org/redirect.php?banner_id=100&url=/
        <http://moblin-contest.org/redirect.php?banner_id=100&url=/>>
                          ------------------------------------------------------------------------

                   _______________________________________________
                   Matplotlib-users mailing list
                   Matplotlib-users@lists.sourceforge.net
        <mailto:Matplotlib-users@lists.sourceforge.net>
                   <mailto:Matplotlib-users@lists.sourceforge.net
        <mailto:Matplotlib-users@lists.sourceforge.net>>

                          https://lists.sourceforge.net/lists/listinfo/matplotlib-users
                   
               -- Jeffrey S. Whitaker Phone : (303)497-6313
               NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449
               325 Broadway Boulder, CO, USA 80305-3328

    -- Jeffrey S. Whitaker Phone : (303)497-6313
    NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449
    325 Broadway Boulder, CO, USA 80305-3328

--
Jeffrey S. Whitaker Phone : (303)497-6313
NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449
325 Broadway Boulder, CO, USA 80305-3328