Plotting grid data

Dear ALL,

Good morning! Here am I again with the first (and perhaps simpler)
part of a potentially long question on plotting gridded data using MPL
(*and* Basemap).

Most examples I have found of plotting gridded data (using either
MATLAB or MPL) depict highly sophisticated 3D plots that are much more
than I can need, which turn to be plotting of data onto a geographic
(therefore, regular and 2D) grid and displaying the plot over a
Basemap (usually, but not necessarily, using an Equirectangular
projection) -- so that I can come up with something like the attached
figure.

To begin with, here are some general questions:

1) Can a plot like this one (but not necessarily identical) be done
with MPL/Basemap from gridded data?

2) For constructing the grid, I have to use the NumPy meshgrid()
method, as the data are regularly spaced on a geographic grid. For
example:

import numpy
import matplotlib.pyplot as plt

coords = numpy.loadtxt('grid.dat')
lon = coords[:,0]
lat = coords[:,1]
dat = coords[:,2]
X, Y = numpy.meshgrid(lon, lat)

where grid.dat is as follows (very simple dataset, just for
demonstration purposes):

-61.05 10.4 20
-79.43 9.15 50
-70.66 9.53 10
-63.11 7.91 40
-63.11 10.55 20
-81.18 7.51 80
-56.48 3.1 90
-60.5 3.93 10
-81.01 7.66 5
-67.43 8.93 10
-65.96 10.31 20
-78.93 8.38 30
-72.86 9.83 40
-68.4 10.61 40
-72.98 10.61 20

The first two columns correspond to longitude, latitude, and the third
correspond to the variable to be plotted onto the grid (species
richness).

The above script reads the data and generates the grid, but then, how
can I display it on a 2D grid overlaid on a Basemap?

Well, as I mentioned above, this is the first part of a longer
question (but I cannot pursue it first before solving these one
first).

Thanks in advance for any assistance you can provide.

With best regards,

wg2.gif

···

--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maurobio@...287...
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."

Mauro Cavalcanti wrote:

Dear ALL,

Good morning! Here am I again with the first (and perhaps simpler)
part of a potentially long question on plotting gridded data using MPL
(*and* Basemap).

Most examples I have found of plotting gridded data (using either
MATLAB or MPL) depict highly sophisticated 3D plots that are much more
than I can need, which turn to be plotting of data onto a geographic
(therefore, regular and 2D) grid and displaying the plot over a
Basemap (usually, but not necessarily, using an Equirectangular
projection) -- so that I can come up with something like the attached
figure.

To begin with, here are some general questions:

1) Can a plot like this one (but not necessarily identical) be done
with MPL/Basemap from gridded data?
  
Mauro: The answer is yes. It looks like a pcolor plot with missing data over the oceans, so you will probably have to use a masked array for the data with the ocean values masked.
2) For constructing the grid, I have to use the NumPy meshgrid()
method, as the data are regularly spaced on a geographic grid. For
example:

import numpy
import matplotlib.pyplot as plt

coords = numpy.loadtxt('grid.dat')
lon = coords[:,0]
lat = coords[:,1]
dat = coords[:,2]
X, Y = numpy.meshgrid(lon, lat)

where grid.dat is as follows (very simple dataset, just for
demonstration purposes):

-61.05 10.4 20
-79.43 9.15 50
-70.66 9.53 10
-63.11 7.91 40
-63.11 10.55 20
-81.18 7.51 80
-56.48 3.1 90
-60.5 3.93 10
-81.01 7.66 5
-67.43 8.93 10
-65.96 10.31 20
-78.93 8.38 30
-72.86 9.83 40
-68.4 10.61 40
-72.98 10.61 20

The first two columns correspond to longitude, latitude, and the third
correspond to the variable to be plotted onto the grid (species
richness).

The above script reads the data and generates the grid, but then, how
can I display it on a 2D grid overlaid on a Basemap?
  

Assuming that this is a regular grid (so that using meshgrid makes sense) you can

using pcolor:

m.pcolormesh(x,y,dat)

using contourf:

m.contourf(x,y,dat,clevs)

using imshow:

m.imshow(dat)

There are examples of all three included with basemap.

HTH,

-Jeff

···

Well, as I mentioned above, this is the first part of a longer
question (but I cannot pursue it first before solving these one
first).

Thanks in advance for any assistance you can provide.

With best regards,

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

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

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you. Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
------------------------------------------------------------------------

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

Dear Jeff,

2008/12/9 Jeff Whitaker <jswhit@...146...>:

Assuming that this is a regular grid (so that using meshgrid makes sense)
you can

using pcolor:
m.pcolormesh(x,y,dat)

Unfortunately, this didn't work. I got the error message (using the
example data from my previous post):

"File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line
6091, in pcolormesh
    C = ma.ravel(C[0:Ny-1, 0:Nx-1]) # data point in each cell is value at
IndexError: too many indices"

using contourf:
m.contourf(x,y,dat,clevs)

Not tried because contour maps are not exactly what I need.

using imshow:
m.imshow(dat)

Another error message:

" File "/usr/lib/python2.5/site-packages/matplotlib/image.py", line
292, in set_data
    raise TypeError("Invalid dimensions for image data")
TypeError: Invalid dimensions for image data"

(Have I to meshgrid the data first?)

There are examples of all three included with basemap.

Indeed, I could not find an example of pcolormesh(). But the
ccsm_popgrid.py example offers a potential solution for another
problem (but this is the second part of my question).

Best wishes,

···

--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maurobio@...287...
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."

Mauro Cavalcanti wrote:

Dear Jeff,

2008/12/9 Jeff Whitaker <jswhit@...146...>:
  

Assuming that this is a regular grid (so that using meshgrid makes sense)
you can
    
using pcolor:
m.pcolormesh(x,y,dat)
    
Unfortunately, this didn't work. I got the error message (using the
example data from my previous post):

"File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line
6091, in pcolormesh
    C = ma.ravel(C[0:Ny-1, 0:Nx-1]) # data point in each cell is value at
IndexError: too many indices"

using contourf:
m.contourf(x,y,dat,clevs)
    
Not tried because contour maps are not exactly what I need.

using imshow:
m.imshow(dat)
    
Another error message:

" File "/usr/lib/python2.5/site-packages/matplotlib/image.py", line
292, in set_data
    raise TypeError("Invalid dimensions for image data")
TypeError: Invalid dimensions for image data"

(Have I to meshgrid the data first?)
  
Mauro: The shape of x, y, and dat must all be (nlats, nlons).
  

There are examples of all three included with basemap.
    
Indeed, I could not find an example of pcolormesh(). But the
ccsm_popgrid.py example offers a potential solution for another
problem (but this is the second part of my question).
  
test.py includes pcolormesh examples (the call signature for pcolor is the same though).

-Jeff

···

Best wishes,

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