Jesper Larsen wrote:

Hi matplotlib users,

I have a long list of ungridded data that I would like to make a contour plot of. The data is simply a list of (longitude, latitude, datavalue) with the data value belonging the given longitude and latitude. As far as I understand contour() only accepts gridded data values.

The solution is probably to interpolate the unstructured data to a regular grid and then plot the data. Has anyone tried doing that or know where to look for an interpolation/triangulation routine?

Cheers,

Jesper

Jesper: Since this question has come up a couple of times, I decided to cook up an example. First you'll need to download and install the natgrid python module (included in CDAT, but I've separated it out from the huge tarball and put it at ftp://ftp.cdc.noaa.gov/Public/jsw/natgrid.tar.gz). Then try this:

from RandomArray import uniform

import pylab as p

import nat

def griddata(x,y,z,xi,yi):

r = nat.Natgrid(y, x, yi, xi)

return r.rgrd(z)

npts = 500

x = uniform(-2,2,npts); y = uniform(-2,2,npts)

z = x*p.exp(-x**2-y**2)

# x, y, and z are now vectors containing nonuniformly sampled data.

# Define a regular grid and grid data to it.

nx = 51; ny = 41

x1 = p.linspace(-2,2,nx)

y1 = p.linspace(-2,2,ny)

xi, yi = p.meshgrid(x1, y1)

zi = griddata(x,y,z,x1,y1)

# Contour the gridded data, plotting dots at the nonuniform data points.

CS = p.contour(xi,yi,zi,15,linewidths=0.5,colors=['k'])

CS = p.contourf(xi,yi,zi,15,cmap=p.cm.jet)

p.scatter(x,y,marker='o',c='b',s=5)

p.xlim(-2,2)

p.ylim(-2,2)

p.show()

It's interesting to see what happens when you vary npts (from 50 to 1000).

HTH,

-Jeff

## ···

--

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

Meteorologist FAX : (303)497-6449

NOAA/OAR/CDC R/CDC1 Email : Jeffrey.S.Whitaker@...259...

325 Broadway Office : Skaggs Research Cntr 1D-124

Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg