Obtaining the contour data in terms of latitude and longitude

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Obtaining the contour data in terms of latitude and longitude

ashwinD12 .
Hello,
         I have data on a 2d lat lon grid(equal grid spacing) in terms of laitude and longitude and heights as enclosed in the image. I need the coordinates(in terms of latitude and longitude) of the contours( as shown in red in the image). From this answer - https://stackoverflow.com/questions/18304722/python-find-contour-lines-from-matplotlib-pyplot-contour/ will this give me what I am looking for or is there a scaling required to latitude and longitude that I need to include?

Best regards,
Ashwin.
import numpy as np

def get_contour_verts(cn):
    contours = []
    # for each contour line
    for cc in cn.collections:
        paths = []
        # for each separate section of the contour line
        for pp in cc.get_paths():
            xy = []
            # for each segment of that section
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)

    return contours



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

sfc_trough1.gif (17K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the contour data in terms of latitude and longitude

Benjamin Root
If you passed the coordinate arrays in with the call to contourf(), then you will get what you expect.

I should note that you should watch out for path simplification (you'll want to turn it off). I also recommend using contourf() over contour() because it handles the edges of the domain better and NaNs.

Cheers!
Ben Root


On Wed, Oct 18, 2017 at 4:59 PM, ashwin .D <[hidden email]> wrote:
Hello,
         I have data on a 2d lat lon grid(equal grid spacing) in terms of laitude and longitude and heights as enclosed in the image. I need the coordinates(in terms of latitude and longitude) of the contours( as shown in red in the image). From this answer - https://stackoverflow.com/questions/18304722/python-find-contour-lines-from-matplotlib-pyplot-contour/ will this give me what I am looking for or is there a scaling required to latitude and longitude that I need to include?

Best regards,
Ashwin.
import numpy as np

def get_contour_verts(cn):
    contours = []
    # for each contour line
    for cc in cn.collections:
        paths = []
        # for each separate section of the contour line
        for pp in cc.get_paths():
            xy = []
            # for each segment of that section
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)

    return contours



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the contour data in terms of latitude and longitude

ashwinD12 .
Ben,
        Thanks ! Can you explain a bit more on what you mean by path simplification ? Here is the code as I see it right now.

Also I had a second question - cs.levels returns the contour levels associated with the contours. Is there a dictionary that maps a contour level to the contour coordinates or is cs.collections in the same order as cs.levels?
import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt

clevs = np.arange(5300.,5900.,20.)
cs = plt.contourf(lons, lats, np.squeeze(hgt),clevs)
for cc in cs.collections:
paths = []
for pp in cc.get_paths():
xy = []
for vv in pp.iter_segments():
xy.append(vv[0])
paths.append(np.vstack(xy))
contours.append(paths)

On Thu, Oct 19, 2017 at 2:38 AM, Benjamin Root <[hidden email]> wrote:
If you passed the coordinate arrays in with the call to contourf(), then you will get what you expect.

I should note that you should watch out for path simplification (you'll want to turn it off). I also recommend using contourf() over contour() because it handles the edges of the domain better and NaNs.

Cheers!
Ben Root


On Wed, Oct 18, 2017 at 4:59 PM, ashwin .D <[hidden email]> wrote:
Hello,
         I have data on a 2d lat lon grid(equal grid spacing) in terms of laitude and longitude and heights as enclosed in the image. I need the coordinates(in terms of latitude and longitude) of the contours( as shown in red in the image). From this answer - https://stackoverflow.com/questions/18304722/python-find-contour-lines-from-matplotlib-pyplot-contour/ will this give me what I am looking for or is there a scaling required to latitude and longitude that I need to include?

Best regards,
Ashwin.
import numpy as np

def get_contour_verts(cn):
    contours = []
    # for each contour line
    for cc in cn.collections:
        paths = []
        # for each separate section of the contour line
        for pp in cc.get_paths():
            xy = []
            # for each segment of that section
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)

    return contours



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users




_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the contour data in terms of latitude and longitude

Ryan May-3
In reply to this post by ashwinD12 .
For this problem, though, I don't think contouring is what you want. I think you'd be better off looking for some spatial consistency in the gradient field directly rather than going through contours.

Ryan

On Wed, Oct 18, 2017 at 2:59 PM, ashwin .D <[hidden email]> wrote:
Hello,
         I have data on a 2d lat lon grid(equal grid spacing) in terms of laitude and longitude and heights as enclosed in the image. I need the coordinates(in terms of latitude and longitude) of the contours( as shown in red in the image). From this answer - https://stackoverflow.com/questions/18304722/python-find-contour-lines-from-matplotlib-pyplot-contour/ will this give me what I am looking for or is there a scaling required to latitude and longitude that I need to include?

Best regards,
Ashwin.
import numpy as np

def get_contour_verts(cn):
    contours = []
    # for each contour line
    for cc in cn.collections:
        paths = []
        # for each separate section of the contour line
        for pp in cc.get_paths():
            xy = []
            # for each segment of that section
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)

    return contours



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users




--
Ryan May


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the contour data in terms of latitude and longitude

ashwinD12 .
In reply to this post by Benjamin Root
Hi Ben,
             I believe have a better idea of my doubt  and I am just thinking loudly here - apologies if all of this is irrelevant. Usually when you have a raster grid  you need to define it with respect to a origin. So since in my case I have a global grid the lower left hand corner is taken to be the origin. This is usually -90 S, 180 W. But you could also have a subset of the global grid in which case the lower left corner's  latitude and longitude can be considered as the origin. This is important especially if you want to know what is grid  orientation of the contour with respect to the origin. I presume I do not need to worry about this with matplotlib's contourf  - am I correct on that or put it differently does it even matter ?

Looking in the code collections.py under lib/matplotlib I see some references to rasterizing a grid and affine transformations. I am not sure whether that is related to any of what I just asked.

Best regards,
Ashwin.

On Thu, Oct 19, 2017 at 2:38 AM, Benjamin Root <[hidden email]> wrote:
If you passed the coordinate arrays in with the call to contourf(), then you will get what you expect.

I should note that you should watch out for path simplification (you'll want to turn it off). I also recommend using contourf() over contour() because it handles the edges of the domain better and NaNs.

Cheers!
Ben Root


On Wed, Oct 18, 2017 at 4:59 PM, ashwin .D <[hidden email]> wrote:
Hello,
         I have data on a 2d lat lon grid(equal grid spacing) in terms of laitude and longitude and heights as enclosed in the image. I need the coordinates(in terms of latitude and longitude) of the contours( as shown in red in the image). From this answer - https://stackoverflow.com/questions/18304722/python-find-contour-lines-from-matplotlib-pyplot-contour/ will this give me what I am looking for or is there a scaling required to latitude and longitude that I need to include?

Best regards,
Ashwin.
import numpy as np

def get_contour_verts(cn):
    contours = []
    # for each contour line
    for cc in cn.collections:
        paths = []
        # for each separate section of the contour line
        for pp in cc.get_paths():
            xy = []
            # for each segment of that section
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)

    return contours



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users




_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the contour data in terms of latitude and longitude

Slavin, Jonathan
In reply to this post by ashwinD12 .
I'm wondering if we could make this easier by modifying contour/contourf so that the ContourSet object created had a paths object attribute.  It doesn't seem like it'd be that hard.  Maybe some would object that it would slow down the creation of contour plots?

Jon

On Wed, Oct 18, 2017 at 5:08 PM, <[hidden email]> wrote:
Date: Thu, 19 Oct 2017 02:29:11 +0530
From: "ashwin .D" <[hidden email]>
To: [hidden email]
Subject: [Matplotlib-users] Obtaining the contour data in terms of
        latitude and longitude
Message-ID:
        <[hidden email]>
Content-Type: text/plain; charset="utf-8"

Hello,
         I have data on a 2d lat lon grid(equal grid spacing) in terms of
laitude and longitude and heights as enclosed in the image. I need the
coordinates(in terms of latitude and longitude) of the contours( as shown
in red in the image). From this answer -
https://stackoverflow.com/questions/18304722/python-find-contour-lines-from-matplotlib-pyplot-contour/
will this give me what I am looking for or is there a scaling required to
latitude and longitude that I need to include?

Best regards,
Ashwin.

import numpy as np
def get_contour_verts(cn):
    contours = []
    # for each contour line
    for cc in cn.collections:
        paths = []
        # for each separate section of the contour line
        for pp in cc.get_paths():
            xy = []
            # for each segment of that section
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)

    return contours
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171019/c5cb0664/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sfc_trough1.gif
Type: image/gif
Size: 13312 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171019/c5cb0664/attachment-0001.gif>

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

Message: 2
Date: Wed, 18 Oct 2017 17:08:16 -0400
From: Benjamin Root <[hidden email]>
To: "ashwin .D" <[hidden email]>
Cc: Matplotlib-users <[hidden email]>
Subject: Re: [Matplotlib-users] Obtaining the contour data in terms of
        latitude and longitude
Message-ID:
        <CANNq6FnAWDDv1vahui+WZY=[hidden email]>
Content-Type: text/plain; charset="utf-8"

If you passed the coordinate arrays in with the call to contourf(), then
you will get what you expect.

I should note that you should watch out for path simplification (you'll
want to turn it off). I also recommend using contourf() over contour()
because it handles the edges of the domain better and NaNs.

Cheers!
Ben Root


On Wed, Oct 18, 2017 at 4:59 PM, ashwin .D <[hidden email]> wrote:

> Hello,
>          I have data on a 2d lat lon grid(equal grid spacing) in terms of
> laitude and longitude and heights as enclosed in the image. I need the
> coordinates(in terms of latitude and longitude) of the contours( as shown
> in red in the image). From this answer - https://stackoverflow.com/
> questions/18304722/python-find-contour-lines-from-
> matplotlib-pyplot-contour/ will this give me what I am looking for or is
> there a scaling required to latitude and longitude that I need to include?
>
> Best regards,
> Ashwin.
>
> import numpy as np
> def get_contour_verts(cn):
>     contours = []
>     # for each contour line
>     for cc in cn.collections:
>         paths = []
>         # for each separate section of the contour line
>         for pp in cc.get_paths():
>             xy = []
>             # for each segment of that section
>             for vv in pp.iter_segments():
>                 xy.append(vv[0])
>             paths.append(np.vstack(xy))
>         contours.append(paths)
>
>     return contours
>
>
>
> _______________________________________________
> Matplotlib-users mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171018/b9f14b69/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


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

End of Matplotlib-users Digest, Vol 27, Issue 24
************************************************



--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: (617) 496-7981       Cambridge, MA 02138-1516
cell: (781) 363-0035             USA
________________________________________________________


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the contour data in terms of latitude and longitude

Ian Thomas-8
See https://github.com/matplotlib/matplotlib/issues/367 for discussion and examples of good and bad ways of doing this.
Ian


On 19 October 2017 at 13:32, Slavin, Jonathan <[hidden email]> wrote:
I'm wondering if we could make this easier by modifying contour/contourf so that the ContourSet object created had a paths object attribute.  It doesn't seem like it'd be that hard.  Maybe some would object that it would slow down the creation of contour plots?

Jon

On Wed, Oct 18, 2017 at 5:08 PM, <[hidden email]> wrote:
Date: Thu, 19 Oct 2017 02:29:11 +0530
From: "ashwin .D" <[hidden email]>
To: [hidden email]
Subject: [Matplotlib-users] Obtaining the contour data in terms of
        latitude and longitude
Message-ID:
        <[hidden email]>
Content-Type: text/plain; charset="utf-8"


Hello,
         I have data on a 2d lat lon grid(equal grid spacing) in terms of
laitude and longitude and heights as enclosed in the image. I need the
coordinates(in terms of latitude and longitude) of the contours( as shown
in red in the image). From this answer -
https://stackoverflow.com/questions/18304722/python-find-contour-lines-from-matplotlib-pyplot-contour/
will this give me what I am looking for or is there a scaling required to
latitude and longitude that I need to include?

Best regards,
Ashwin.

import numpy as np
def get_contour_verts(cn):
    contours = []
    # for each contour line
    for cc in cn.collections:
        paths = []
        # for each separate section of the contour line
        for pp in cc.get_paths():
            xy = []
            # for each segment of that section
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)

    return contours
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171019/c5cb0664/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sfc_trough1.gif
Type: image/gif
Size: 13312 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171019/c5cb0664/attachment-0001.gif>

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

Message: 2
Date: Wed, 18 Oct 2017 17:08:16 -0400
From: Benjamin Root <[hidden email]>
To: "ashwin .D" <[hidden email]>
Cc: Matplotlib-users <[hidden email]>
Subject: Re: [Matplotlib-users] Obtaining the contour data in terms of
        latitude and longitude
Message-ID:
        <CANNq6FnAWDDv1vahui+WZY=[hidden email]>
Content-Type: text/plain; charset="utf-8"


If you passed the coordinate arrays in with the call to contourf(), then
you will get what you expect.

I should note that you should watch out for path simplification (you'll
want to turn it off). I also recommend using contourf() over contour()
because it handles the edges of the domain better and NaNs.

Cheers!
Ben Root


On Wed, Oct 18, 2017 at 4:59 PM, ashwin .D <[hidden email]> wrote:

> Hello,
>          I have data on a 2d lat lon grid(equal grid spacing) in terms of
> laitude and longitude and heights as enclosed in the image. I need the
> coordinates(in terms of latitude and longitude) of the contours( as shown
> in red in the image). From this answer - https://stackoverflow.com/
> questions/18304722/python-find-contour-lines-from-
> matplotlib-pyplot-contour/ will this give me what I am looking for or is
> there a scaling required to latitude and longitude that I need to include?
>
> Best regards,
> Ashwin.
>
> import numpy as np
> def get_contour_verts(cn):
>     contours = []
>     # for each contour line
>     for cc in cn.collections:
>         paths = []
>         # for each separate section of the contour line
>         for pp in cc.get_paths():
>             xy = []
>             # for each segment of that section
>             for vv in pp.iter_segments():
>                 xy.append(vv[0])
>             paths.append(np.vstack(xy))
>         contours.append(paths)
>
>     return contours
>
>
>
> _______________________________________________
> Matplotlib-users mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171018/b9f14b69/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


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

End of Matplotlib-users Digest, Vol 27, Issue 24
************************************************



--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: <a href="tel:(617)%20496-7981" value="+16174967981" target="_blank">(617) 496-7981       Cambridge, MA 02138-1516
cell: <a href="tel:(781)%20363-0035" value="+17813630035" target="_blank">(781) 363-0035             USA
________________________________________________________


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users