basemap and contouring

Hi,

If I set up a random example and plot it as a contour it seems to work
fine...e.g.

import matplotlb.pyplot as plt
import numpy as np

data2 = np.random.sample((17,16))
x = np.linspace(-1.74, -1.32, 16)
y = np.linspace(15.15, 15.61, 17)
X, Y = np.meshgrid(x, y)
plt.contour(X, Y, data2, colors='black')
plt.show()

However if instead I try use a basemap projection to do the contouring I run
into trouble. For example...

import numpy as np
from mpl_toolkits.basemap import Basemap

ulat, llat, ulon, llon = 15.61, 15.15, -1.32, -1.74
m = Basemap(projection='geos', lon_0=0.0, llcrnrlon=llon,
                     llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat,
                     resolution='c')

data2 = np.random.sample((17,16))
x = np.linspace(-1.74, -1.32, 16)
y = np.linspace(15.15, 15.61, 17)
X, Y = m(x, y)
m.contourf(X, Y, data2, colors='black')
plt.show()

The errors I am getting are....

x, y = m(lons, lats)
  File "/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/__init__.py",
line 823, in __call__
    return self.projtran(x,y,inverse=inverse)
  File "/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/proj.py",
line 241, in __call__
    outx,outy = self._proj4(x, y, inverse=inverse)
  File "/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/pyproj.py",
line 193, in __call__
    _Proj._fwd(self, inx, iny, radians=radians, errcheck=errcheck)
  File "_proj.pyx", line 56, in _proj.Proj._fwd (src/_proj.c:725)
RuntimeError: Buffer lengths not the same

Many thanks,

Martin

···

--
View this message in context: http://old.nabble.com/basemap-and-contouring-tp29697864p29697864.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

You replaced the call to meshgrid with a call to the Basemap object.
You need to use both:

px,py = m(x, y)
X,Y = np.meshgrid(px, py)

Ryan

···

On Mon, Sep 13, 2010 at 7:45 AM, mdekauwe <mdekauwe@...287...> wrote:

Hi,

If I set up a random example and plot it as a contour it seems to work
fine...e.g.

import matplotlb.pyplot as plt
import numpy as np

data2 = np.random.sample((17,16))
x = np.linspace(-1.74, -1.32, 16)
y = np.linspace(15.15, 15.61, 17)
X, Y = np.meshgrid(x, y)
plt.contour(X, Y, data2, colors='black')
plt.show()

However if instead I try use a basemap projection to do the contouring I run
into trouble. For example...

import numpy as np
from mpl_toolkits.basemap import Basemap

ulat, llat, ulon, llon = 15.61, 15.15, -1.32, -1.74
m = Basemap(projection='geos', lon_0=0.0, llcrnrlon=llon,
llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat,
resolution='c')

data2 = np.random.sample((17,16))
x = np.linspace(-1.74, -1.32, 16)
y = np.linspace(15.15, 15.61, 17)
X, Y = m(x, y)
m.contourf(X, Y, data2, colors='black')
plt.show()

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

Hi,

Well hopefully doing what you suggested correctly...

numrows = 17
numcols = 16
ulat, llat, ulon, llon = 15.61, 15.15, -1.32, -1.74
m = Basemap(projection='geos', lon_0=0.0, llcrnrlon=llon,
                      llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat,
                      resolution='c')
    
data2 = np.random.sample((17,16))
x = np.linspace(-1.74, -1.32, 16)
y = np.linspace(15.15, 15.61, 17)
px, py = m(x, y)
X, Y = np.meshgrid(px, py)
m.contourf(X, Y, data2, colors='black')
plt.show()

Doesn't seem to work either?

thanks,

Martin

Ryan May-3 wrote:

···

On Mon, Sep 13, 2010 at 7:45 AM, mdekauwe <mdekauwe@...287...> wrote:

Hi,

If I set up a random example and plot it as a contour it seems to work
fine...e.g.

import matplotlb.pyplot as plt
import numpy as np

data2 = np.random.sample((17,16))
x = np.linspace(-1.74, -1.32, 16)
y = np.linspace(15.15, 15.61, 17)
X, Y = np.meshgrid(x, y)
plt.contour(X, Y, data2, colors='black')
plt.show()

However if instead I try use a basemap projection to do the contouring I
run
into trouble. For example...

import numpy as np
from mpl_toolkits.basemap import Basemap

ulat, llat, ulon, llon = 15.61, 15.15, -1.32, -1.74
m = Basemap(projection='geos', lon_0=0.0, llcrnrlon=llon,
                    llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat,
                    resolution='c')

data2 = np.random.sample((17,16))
x = np.linspace(-1.74, -1.32, 16)
y = np.linspace(15.15, 15.61, 17)
X, Y = m(x, y)
m.contourf(X, Y, data2, colors='black')
plt.show()

You replaced the call to meshgrid with a call to the Basemap object.
You need to use both:

px,py = m(x, y)
X,Y = np.meshgrid(px, py)

Ryan

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
View this message in context: http://old.nabble.com/basemap-and-contouring-tp29697864p29698213.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Martin,

Double-check the shape of the X and the Y arrays. Make sure they have the same shape as data2. I would be willing to bet that some of the shapes got reversed.

Ben Root

···

On Mon, Sep 13, 2010 at 8:21 AM, mdekauwe <mdekauwe@…287…> wrote:

Hi,

Well hopefully doing what you suggested correctly…

numrows = 17

numcols = 16
ulat, llat, ulon, llon = 15.61, 15.15, -1.32, -1.74

m = Basemap(projection=‘geos’, lon_0=0.0, llcrnrlon=llon,

                  llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat,

                  resolution='c')

data2 = np.random.sample((17,16))

x = np.linspace(-1.74, -1.32, 16)

y = np.linspace(15.15, 15.61, 17)

px, py = m(x, y)

X, Y = np.meshgrid(px, py)

m.contourf(X, Y, data2, colors=‘black’)

plt.show()

Doesn’t seem to work either?

thanks,

Martin

Hi,

Well I tried...

numrows = 17
numcols = 16
ulat, llat, ulon, llon = 15.61, 15.15, -1.32, -1.74
m = Basemap(projection='geos', lon_0=0.0, llcrnrlon=llon,
                llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat,
                resolution='c')

data2 = np.random.sample((17,16))
print 'DATA2 shape:', data2.shape
x = np.linspace(-1.74, -1.32, 16)
y = np.linspace(15.15, 15.61, 17)
print 'x shape:', x.shape
print 'y shape:', y.shape
px, py = m(x, y)

which gives...

DATA2 shape: (17, 16)
x shape: (16,)
y shape: (17,)

So that looks OK. Turns out the error now comes at the line px, py = m(x,
y). So perhaps I am doing the basemap step incorrectly?

Traceback (most recent call last):
  File "./recontour_plot.py", line 83, in <module>
    px, py = m(x, y)
  File "/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/__init__.py",
line 823, in __call__
    return self.projtran(x,y,inverse=inverse)
  File "/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/proj.py",
line 241, in __call__
    outx,outy = self._proj4(x, y, inverse=inverse)
  File "/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/pyproj.py",
line 193, in __call__
    _Proj._fwd(self, inx, iny, radians=radians, errcheck=errcheck)
  File "_proj.pyx", line 56, in _proj.Proj._fwd (src/_proj.c:725)
RuntimeError: Buffer lengths not the same

Benjamin Root-2 wrote:

···

On Mon, Sep 13, 2010 at 8:21 AM, mdekauwe <mdekauwe@...287...> wrote:

Hi,

Well hopefully doing what you suggested correctly...

numrows = 17
numcols = 16
ulat, llat, ulon, llon = 15.61, 15.15, -1.32, -1.74
m = Basemap(projection='geos', lon_0=0.0, llcrnrlon=llon,
                     llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat,
                     resolution='c')

data2 = np.random.sample((17,16))
x = np.linspace(-1.74, -1.32, 16)
y = np.linspace(15.15, 15.61, 17)
px, py = m(x, y)
X, Y = np.meshgrid(px, py)
m.contourf(X, Y, data2, colors='black')
plt.show()

Doesn't seem to work either?

thanks,

Martin

Martin,

Double-check the shape of the X and the Y arrays. Make sure they have the
same shape as data2. I would be willing to bet that some of the shapes
got
reversed.

Ben Root

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing
http://p.sf.net/sfu/novell-sfdev2dev

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
View this message in context: http://old.nabble.com/basemap-and-contouring-tp29697864p29698610.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Martin:

You need a meshgrid

x,y = np.meshgrid(x,y)

just *before* (not after)

px,py = m(x,y)

so that x and y have the same shape as data2.

-Jeff

···

On 9/13/10 8:02 AM, mdekauwe wrote:

Hi,

Well I tried...

numrows = 17
numcols = 16
ulat, llat, ulon, llon = 15.61, 15.15, -1.32, -1.74
m = Basemap(projection='geos', lon_0=0.0, llcrnrlon=llon,
                 llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat,
                 resolution='c')

data2 = np.random.sample((17,16))
print 'DATA2 shape:', data2.shape
x = np.linspace(-1.74, -1.32, 16)
y = np.linspace(15.15, 15.61, 17)
print 'x shape:', x.shape
print 'y shape:', y.shape
px, py = m(x, y)

which gives...

DATA2 shape: (17, 16)
x shape: (16,)
y shape: (17,)

So that looks OK. Turns out the error now comes at the line px, py = m(x,
y). So perhaps I am doing the basemap step incorrectly?

Traceback (most recent call last):
   File "./recontour_plot.py", line 83, in<module>
     px, py = m(x, y)
   File "/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/__init__.py",
line 823, in __call__
     return self.projtran(x,y,inverse=inverse)
   File "/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/proj.py",
line 241, in __call__
     outx,outy = self._proj4(x, y, inverse=inverse)
   File "/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/pyproj.py",
line 193, in __call__
     _Proj._fwd(self, inx, iny, radians=radians, errcheck=errcheck)
   File "_proj.pyx", line 56, in _proj.Proj._fwd (src/_proj.c:725)
RuntimeError: Buffer lengths not the same

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

Hi Jeff,

many thanks that was exactly the problem!

Martin

Jeff Whitaker wrote:

···

  On 9/13/10 8:02 AM, mdekauwe wrote:

Hi,

Well I tried...

numrows = 17
numcols = 16
ulat, llat, ulon, llon = 15.61, 15.15, -1.32, -1.74
m = Basemap(projection='geos', lon_0=0.0, llcrnrlon=llon,
                 llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat,
                 resolution='c')

data2 = np.random.sample((17,16))
print 'DATA2 shape:', data2.shape
x = np.linspace(-1.74, -1.32, 16)
y = np.linspace(15.15, 15.61, 17)
print 'x shape:', x.shape
print 'y shape:', y.shape
px, py = m(x, y)

which gives...

DATA2 shape: (17, 16)
x shape: (16,)
y shape: (17,)

So that looks OK. Turns out the error now comes at the line px, py = m(x,
y). So perhaps I am doing the basemap step incorrectly?

Traceback (most recent call last):
   File "./recontour_plot.py", line 83, in<module>
     px, py = m(x, y)
   File
"/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/__init__.py",
line 823, in __call__
     return self.projtran(x,y,inverse=inverse)
   File "/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/proj.py",
line 241, in __call__
     outx,outy = self._proj4(x, y, inverse=inverse)
   File
"/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/pyproj.py",
line 193, in __call__
     _Proj._fwd(self, inx, iny, radians=radians, errcheck=errcheck)
   File "_proj.pyx", line 56, in _proj.Proj._fwd (src/_proj.c:725)
RuntimeError: Buffer lengths not the same

Martin:

You need a meshgrid

x,y = np.meshgrid(x,y)

just *before* (not after)

px,py = m(x,y)

so that x and y have the same shape as data2.

-Jeff

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
View this message in context: http://old.nabble.com/basemap-and-contouring-tp29697864p29699166.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Martin,

It would be more useful to see what are the shapes of px, py, X, and Y.

Ben Root

···

On Mon, Sep 13, 2010 at 9:02 AM, mdekauwe <mdekauwe@…287…> wrote:

Hi,

Well I tried…

numrows = 17

numcols = 16

ulat, llat, ulon, llon = 15.61, 15.15, -1.32, -1.74

m = Basemap(projection=‘geos’, lon_0=0.0, llcrnrlon=llon,

            llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat,

            resolution='c')

data2 = np.random.sample((17,16))

print ‘DATA2 shape:’, data2.shape
x = np.linspace(-1.74, -1.32, 16)

y = np.linspace(15.15, 15.61, 17)

print ‘x shape:’, x.shape

print ‘y shape:’, y.shape
px, py = m(x, y)

which gives…

DATA2 shape: (17, 16)

x shape: (16,)

y shape: (17,)

So that looks OK. Turns out the error now comes at the line px, py = m(x,

y). So perhaps I am doing the basemap step incorrectly?

Traceback (most recent call last):

File “./recontour_plot.py”, line 83, in
px, py = m(x, y)

File “/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/init.py”,

line 823, in call

return self.projtran(x,y,inverse=inverse)

File “/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/proj.py”,

line 241, in call

outx,outy = self._proj4(x, y, inverse=inverse)

File “/users/eow/mgdk/sun4u//lib/python/mpl_toolkits/basemap/pyproj.py”,

line 193, in call

_Proj._fwd(self, inx, iny, radians=radians, errcheck=errcheck)

File “_proj.pyx”, line 56, in _proj.Proj._fwd (src/_proj.c:725)

RuntimeError: Buffer lengths not the same

Yeah, my bad. That's what I get for responding *before* coffee this morning.

Ryan

···

On Mon, Sep 13, 2010 at 9:23 AM, Jeff Whitaker <jswhit@...146...> wrote:

Martin:

You need a meshgrid

x,y = np.meshgrid(x,y)

just *before* (not after)

px,py = m(x,y)

so that x and y have the same shape as data2.

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma