plotting a contour map from CSV file

Dear Matplotlib-Users,
I am tryring to create a contour plot over a basemap.

My main problem is creating the array for the Z values as a basis for the
plt.contour command from a CSV file where latitude, longitude and value are
stored column-wise:

lat; lon; value
50; 10; 6
...

The data represents a regular spaced grid with a datapoint each 0.25 degrees.

I tried various possibilities but didn't have success:

1) following simpletest.py from the basemap examples:
X, Y = meshgrid(data[:,1], data[:,0])

Z = data[:,2]

m.contourf(x,y, Z)

=> Error: Z must be a 2D array
-> How do I get Z to be a 2D array?

2) using the griddata package
Here I was nearly without orientation how to call griddata correctly.

3) Using the python bindings of ogr
Any examples on this one?

From my above demonstrated methods the following questions arrise:

What is the preferred way to plot
  - Points stored in the above descripbed format (lat, lon, value)?
  - Interpolate a grid of data points by using different interpolation methods
like inverse distance wheighting, natural neighbor interpolation, etc. to get a
contour map?

Thanks in advance for your help & kind regards,
Timmie

Tim Michelsen wrote:

Dear Matplotlib-Users,
I am tryring to create a contour plot over a basemap.

My main problem is creating the array for the Z values as a basis for the
plt.contour command from a CSV file where latitude, longitude and value are
stored column-wise:

lat; lon; value
50; 10; 6
...

The data represents a regular spaced grid with a datapoint each 0.25 degrees.

I tried various possibilities but didn't have success:

1) following simpletest.py from the basemap examples:
X, Y = meshgrid(data[:,1], data[:,0])

Z = data[:,2]
  

Timmie: Try:

X, Y = meshgrid(data[:,1], data[:,0])
Z = data[:,2]
nlons = X.shape[1]; nlats = X.shape[0]
Z = Z.reshape(nlats,nlons)

m.contourf(x,y, Z)

=> Error: Z must be a 2D array
-> How do I get Z to be a 2D array?

2) using the griddata package
Here I was nearly without orientation how to call griddata correctly.
  

You don't need to use griddata since you have regularly gridded data.

3) Using the python bindings of ogr
Any examples on this one?
  

Again, no need. A simple reshape will get you the 2d lat/lon array you need.

>From my above demonstrated methods the following questions arrise:
What is the preferred way to plot
  - Points stored in the above descripbed format (lat, lon, value)?
  - Interpolate a grid of data points by using different interpolation methods
like inverse distance wheighting, natural neighbor interpolation, etc. to get a
contour map?
  
For interpolation of irregular, randomly distributed data points see
http://www.scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data.

However, if there is some structure to the data grid then it's probably better not to use these approaches.

-Jeff

···

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

Jeff Whitaker wrote:

Tim Michelsen wrote:
  

Dear Matplotlib-Users,
I am tryring to create a contour plot over a basemap.

My main problem is creating the array for the Z values as a basis for the
plt.contour command from a CSV file where latitude, longitude and value are
stored column-wise:

lat; lon; value
50; 10; 6
...

The data represents a regular spaced grid with a datapoint each 0.25 degrees.

I tried various possibilities but didn't have success:

1) following simpletest.py from the basemap examples:
X, Y = meshgrid(data[:,1], data[:,0])

Z = data[:,2]
  

Timmie: Try:

X, Y = meshgrid(data[:,1], data[:,0])
Z = data[:,2]
nlons = X.shape[1]; nlats = X.shape[0]
Z = Z.reshape(nlats,nlons)
  
Timmie: Sorry, but upon further reflection I don't think this will work. You'll need to know the number of
lats and the number of lons on the grid beforehand. Then you should be able to do

X = X.reshape(nlats,nlons)
Y = Y.reshape(nlats,nlons)
Z = Z.reshape(nlats,nlons)

after reading the data in.

(skip the meshgrid call, that's only useful when X is a vector with length nlons and Y is a vector with length nlats).

If you still have problems, send us a full example.

-Jeff

···

  

m.contourf(x,y, Z)

=> Error: Z must be a 2D array
-> How do I get Z to be a 2D array?

2) using the griddata package
Here I was nearly without orientation how to call griddata correctly.
  

You don't need to use griddata since you have regularly gridded data.
  

3) Using the python bindings of ogr
Any examples on this one?
  

Again, no need. A simple reshape will get you the 2d lat/lon array you need.

>From my above demonstrated methods the following questions arrise:
What is the preferred way to plot
  - Points stored in the above descripbed format (lat, lon, value)?
  - Interpolate a grid of data points by using different interpolation methods
like inverse distance wheighting, natural neighbor interpolation, etc. to get a
contour map?
  
For interpolation of irregular, randomly distributed data points see
http://www.scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data.

However, if there is some structure to the data grid then it's probably better not to use these approaches.

-Jeff

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