plotting a contour map from CSV file

Hello,
thanks.
I checked again from contour_demo.py of the basemap distribution.

There lats, lons are uniquely monoton increasing from 0-360 and from -90 to 90.
In my case data is written row-by-row:
* increasing from lowest latitude western most longitude to easternmost
longitude and then increasing each rows in the same manner to the northermost
latitude (see below).

So, as you said, it's a question of re-aranging the data. that it fits the to
the way m.contour expects the 2-D array.
Also, since the grid is still coarse, I would need to apply some smoothing
afterwards. What do you recommend for that?

I don't know how I can do this easily by hand. May you give me some guidance
here, please?

But I may just convert it to a shape file using GIS then load it with the
shapefile interface you wrote.
What would you see as most convenient way?
If I produce maps with a GIS but want to use matplotlib for the map plotting,
what would be the preferred export format? Any gdal format?

Many thanks in advance,
Timmie

### data example

Latitude Longitude value
45 7 7.65251434
45 7.25 6.841345477
45 7.5 3.923153289
45 7.75 3.644313708
45 8 3.550977951
45 8.25 3.352525137
45 8.5 3.080082094
45 8.75 2.971992657
45 9 2.998723785
45 9.25 3.080082094
45 9.5 3.185687405
45 9.75 3.102075854
45 10 3.185687405
45 10.25 3.213960325
45 10.5 3.32326373
45 10.75 3.465643983
45 11 3.612980369
45 11.25 3.644313708
45 11.5 3.701277511
45 11.75 3.923153289
45 12 3.797848342
45 12.25 3.612980369
45 12.5 3.435577844
45 12.75 3.294210812
45 13 3.26536503
45.25 7 6.485050223
45.25 7.25 6.343081631
45.25 7.5 3.856783573
45.25 7.75 3.405725407
45.25 8 3.550977951
45.25 8.25 3.294210812
45.25 8.5 3.294210812
45.25 8.75 3.185687405
45.25 9 3.15761656
45.25 9.25 3.213960325
45.25 9.5 3.15761656
45.25 9.75 3.32326373
45.25 10 3.405725407
45.25 10.25 3.495925216
45.25 10.5 3.465643983
45.25 10.75 3.550977951
45.25 11 3.465643983
45.25 11.25 3.765429652
45.25 11.5 3.95669157
45.25 11.75 3.797848342
45.25 12 3.923153289
45.25 12.25 3.733239867
45.25 12.5 3.550977951
45.25 12.75 3.520306012
45.25 13 3.376085288
45.5 7 6.383367092
45.5 7.25 6.383367092
45.5 7.5 6.009422688
45.5 7.75 4.679469855
45.5 8 3.435577844
45.5 8.25 3.435577844
45.5 8.5 3.236725042
45.5 8.75 3.236725042
45.5 9 3.185687405
45.5 9.25 3.102075854
45.5 9.5 3.102075854
45.5 9.75 3.185687405
45.5 10 3.352525137
45.5 10.25 3.405725407
45.5 10.5 3.376085288
45.5 10.75 3.612980369
45.5 11 3.520306012
45.5 11.25 3.352525137
45.5 11.5 3.823949103
45.5 11.75 3.856783573
45.5 12 3.856783573
45.5 12.25 3.765429652
45.5 12.5 3.669541114
45.5 12.75 3.550977951
45.5 13 3.435577844
45.75 7 5.309043916
45.75 7.25 6.057519881
45.75 7.5 5.030958443
45.75 7.75 4.836570243
45.75 8 4.836570243
45.75 8.25 2.724965001
45.75 8.5 2.607751091
45.75 8.75 3.26536503
45.75 9 2.898163214
45.75 9.25 2.872155245
45.75 9.5 1.893252754
45.75 9.75 2.043669061
45.75 10 1.75488883
45.75 10.25 2.004264146
45.75 10.5 2.971992657
45.75 10.75 1.804949998
45.75 11 2.846334614
45.75 11.25 5.519419657
45.75 11.5 2.517818813
45.75 11.75 3.733239867
45.75 12 3.376085288
45.75 12.25 3.550977951
45.75 12.5 3.612980369
45.75 12.75 3.520306012
45.75 13 3.495925216
46 7 5.06399168
46 7.25 4.949174095
46 7.5 5.266087828
46 7.75 5.352298328
46 8 4.757472437
46 8.25 2.800325674
46 8.5 3.612980369
46 8.75 3.185687405
46 9 2.323282473
46 9.25 1.671485743
46 9.5 3.856783573
46 9.75 4.572079662
46 10 4.679469855
46 10.25 4.679469855
46 10.5 5.309043916
46 10.75 3.294210812
46 11 3.405725407
46 11.25 3.669541114
46 11.5 3.495925216
46 11.75 4.255093726
46 12 3.495925216
46 12.25 3.185687405
46 12.5 3.213960325
46 12.75 3.550977951
46 13 3.520306012
46.25 7 1.969297411
46.25 7.25 4.908706364
46.25 7.5 3.052767233
46.25 7.75 3.765429652
46.25 8 3.95669157
46.25 8.25 5.06399168
46.25 8.5 5.266087828
46.25 8.75 3.669541114
46.25 9 3.185687405
46.25 9.25 3.797848342
46.25 9.5 3.352525137
46.25 9.75 5.439709782
46.25 10 5.69098301
46.25 10.25 4.949174095
46.25 10.5 5.736883145
46.25 10.75 5.105542055
46.25 11 4.255093726
46.25 11.25 3.701277511
46.25 11.5 4.255093726
46.25 11.75 4.572079662
46.25 12 3.98369323
46.25 12.25 4.148941623
46.25 12.5 3.129746478
46.25 12.75 3.236725042
46.25 13 3.550977951
46.5 7 2.872155245
46.5 7.25 3.701277511
46.5 7.5 3.15761656
46.5 7.75 3.765429652
46.5 8 5.18951259
46.5 8.25 6.105948261
46.5 8.5 5.266087828
46.5 8.75 5.69098301
46.5 9 6.009422688
46.5 9.25 5.147381739
46.5 9.5 5.829636932
46.5 9.75 5.654489904
46.5 10 6.243327668
46.5 10.25 5.395852976
46.5 10.5 5.736883145
46.5 10.75 6.057519881
46.5 11 5.147381739
46.5 11.25 3.520306012
46.5 11.5 3.856783573
46.5 11.75 4.148941623
46.5 12 4.71833512
46.5 12.25 4.71833512
46.5 12.5 3.701277511
46.5 12.75 3.889851131
46.5 13 3.32326373
46.75 7 1.859766825
46.75 7.25 2.198852355
46.75 7.5 2.345277833
46.75 7.75 2.517818813
46.75 8 3.856783573
46.75 8.25 3.856783573
46.75 8.5 5.06399168
46.75 8.75 4.184077131
46.75 9 5.829636932
46.75 9.25 3.644313708
46.75 9.5 3.765429652
46.75 9.75 5.309043916
46.75 10 6.009422688
46.75 10.25 5.147381739
46.75 10.5 5.609155594
46.75 10.75 5.783100444
46.75 11 5.147381739
46.75 11.25 3.581868928
46.75 11.5 4.908706364
46.75 11.75 3.465643983
46.75 12 3.465643983
46.75 12.25 4.148941623
46.75 12.5 3.98369323
46.75 12.75 3.581868928
46.75 13 3.644313708

Tim Michelsen wrote:

Hello,
thanks.
I checked again from contour_demo.py of the basemap distribution.

There lats, lons are uniquely monoton increasing from 0-360 and from -90 to 90.
In my case data is written row-by-row:
* increasing from lowest latitude western most longitude to easternmost
longitude and then increasing each rows in the same manner to the northermost
latitude (see below).

So, as you said, it's a question of re-aranging the data. that it fits the to
the way m.contour expects the 2-D array.
Also, since the grid is still coarse, I would need to apply some smoothing
afterwards. What do you recommend for that?
  

Timme: Here's one way to do it

from matplotlib.mlab import load
import matplotlib.pyplot as plt
import numpy as np
data = load("data.txt")
# need to know nlons and nlats beforehand!
nlons = 8; nlats = 25
X = data[0::nlats,0]
Y = data[0:nlats,1]
# data is in nlons,nlats order in file, need to transpose
Z = data[:,2].reshape(nlons,nlats).transpose()
X,Y = np.meshgrid(X,Y)
CS = plt.contourf(X,Y,Z,20)
plt.show()

I don't have any recommendations for smoothing - why don't you plot the raw data first and see if you really need it?

I don't know how I can do this easily by hand. May you give me some guidance
here, please?

But I may just convert it to a shape file using GIS then load it with the
shapefile interface you wrote.
What would you see as most convenient way?
If I produce maps with a GIS but want to use matplotlib for the map plotting,
what would be the preferred export format? Any gdal format?
  

I prefer netCDF format for gridded data (basemap contains a function for reading netCDF files - NetCDFFile).

-Jeff

···

Many thanks in advance,
Timmie

### data example

Latitude Longitude value
45 7 7.65251434
45 7.25 6.841345477
45 7.5 3.923153289
45 7.75 3.644313708
45 8 3.550977951
45 8.25 3.352525137
45 8.5 3.080082094
45 8.75 2.971992657
45 9 2.998723785
45 9.25 3.080082094
45 9.5 3.185687405
45 9.75 3.102075854
45 10 3.185687405
45 10.25 3.213960325
45 10.5 3.32326373
45 10.75 3.465643983
45 11 3.612980369
45 11.25 3.644313708
45 11.5 3.701277511
45 11.75 3.923153289
45 12 3.797848342
45 12.25 3.612980369
45 12.5 3.435577844
45 12.75 3.294210812
45 13 3.26536503
45.25 7 6.485050223
45.25 7.25 6.343081631
45.25 7.5 3.856783573
45.25 7.75 3.405725407
45.25 8 3.550977951
45.25 8.25 3.294210812
45.25 8.5 3.294210812
45.25 8.75 3.185687405
45.25 9 3.15761656
45.25 9.25 3.213960325
45.25 9.5 3.15761656
45.25 9.75 3.32326373
45.25 10 3.405725407
45.25 10.25 3.495925216
45.25 10.5 3.465643983
45.25 10.75 3.550977951
45.25 11 3.465643983
45.25 11.25 3.765429652
45.25 11.5 3.95669157
45.25 11.75 3.797848342
45.25 12 3.923153289
45.25 12.25 3.733239867
45.25 12.5 3.550977951
45.25 12.75 3.520306012
45.25 13 3.376085288
45.5 7 6.383367092
45.5 7.25 6.383367092
45.5 7.5 6.009422688
45.5 7.75 4.679469855
45.5 8 3.435577844
45.5 8.25 3.435577844
45.5 8.5 3.236725042
45.5 8.75 3.236725042
45.5 9 3.185687405
45.5 9.25 3.102075854
45.5 9.5 3.102075854
45.5 9.75 3.185687405
45.5 10 3.352525137
45.5 10.25 3.405725407
45.5 10.5 3.376085288
45.5 10.75 3.612980369
45.5 11 3.520306012
45.5 11.25 3.352525137
45.5 11.5 3.823949103
45.5 11.75 3.856783573
45.5 12 3.856783573
45.5 12.25 3.765429652
45.5 12.5 3.669541114
45.5 12.75 3.550977951
45.5 13 3.435577844
45.75 7 5.309043916
45.75 7.25 6.057519881
45.75 7.5 5.030958443
45.75 7.75 4.836570243
45.75 8 4.836570243
45.75 8.25 2.724965001
45.75 8.5 2.607751091
45.75 8.75 3.26536503
45.75 9 2.898163214
45.75 9.25 2.872155245
45.75 9.5 1.893252754
45.75 9.75 2.043669061
45.75 10 1.75488883
45.75 10.25 2.004264146
45.75 10.5 2.971992657
45.75 10.75 1.804949998
45.75 11 2.846334614
45.75 11.25 5.519419657
45.75 11.5 2.517818813
45.75 11.75 3.733239867
45.75 12 3.376085288
45.75 12.25 3.550977951
45.75 12.5 3.612980369
45.75 12.75 3.520306012
45.75 13 3.495925216
46 7 5.06399168
46 7.25 4.949174095
46 7.5 5.266087828
46 7.75 5.352298328
46 8 4.757472437
46 8.25 2.800325674
46 8.5 3.612980369
46 8.75 3.185687405
46 9 2.323282473
46 9.25 1.671485743
46 9.5 3.856783573
46 9.75 4.572079662
46 10 4.679469855
46 10.25 4.679469855
46 10.5 5.309043916
46 10.75 3.294210812
46 11 3.405725407
46 11.25 3.669541114
46 11.5 3.495925216
46 11.75 4.255093726
46 12 3.495925216
46 12.25 3.185687405
46 12.5 3.213960325
46 12.75 3.550977951
46 13 3.520306012
46.25 7 1.969297411
46.25 7.25 4.908706364
46.25 7.5 3.052767233
46.25 7.75 3.765429652
46.25 8 3.95669157
46.25 8.25 5.06399168
46.25 8.5 5.266087828
46.25 8.75 3.669541114
46.25 9 3.185687405
46.25 9.25 3.797848342
46.25 9.5 3.352525137
46.25 9.75 5.439709782
46.25 10 5.69098301
46.25 10.25 4.949174095
46.25 10.5 5.736883145
46.25 10.75 5.105542055
46.25 11 4.255093726
46.25 11.25 3.701277511
46.25 11.5 4.255093726
46.25 11.75 4.572079662
46.25 12 3.98369323
46.25 12.25 4.148941623
46.25 12.5 3.129746478
46.25 12.75 3.236725042
46.25 13 3.550977951
46.5 7 2.872155245
46.5 7.25 3.701277511
46.5 7.5 3.15761656
46.5 7.75 3.765429652
46.5 8 5.18951259
46.5 8.25 6.105948261
46.5 8.5 5.266087828
46.5 8.75 5.69098301
46.5 9 6.009422688
46.5 9.25 5.147381739
46.5 9.5 5.829636932
46.5 9.75 5.654489904
46.5 10 6.243327668
46.5 10.25 5.395852976
46.5 10.5 5.736883145
46.5 10.75 6.057519881
46.5 11 5.147381739
46.5 11.25 3.520306012
46.5 11.5 3.856783573
46.5 11.75 4.148941623
46.5 12 4.71833512
46.5 12.25 4.71833512
46.5 12.5 3.701277511
46.5 12.75 3.889851131
46.5 13 3.32326373
46.75 7 1.859766825
46.75 7.25 2.198852355
46.75 7.5 2.345277833
46.75 7.75 2.517818813
46.75 8 3.856783573
46.75 8.25 3.856783573
46.75 8.5 5.06399168
46.75 8.75 4.184077131
46.75 9 5.829636932
46.75 9.25 3.644313708
46.75 9.5 3.765429652
46.75 9.75 5.309043916
46.75 10 6.009422688
46.75 10.25 5.147381739
46.75 10.5 5.609155594
46.75 10.75 5.783100444
46.75 11 5.147381739
46.75 11.25 3.581868928
46.75 11.5 4.908706364
46.75 11.75 3.465643983
46.75 12 3.465643983
46.75 12.25 4.148941623
46.75 12.5 3.98369323
46.75 12.75 3.581868928
46.75 13 3.644313708

-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
  
--
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

Hello Jeff,

Timme: Here's one way to do it

many thanks so far. I still have to inspect and improve my script. But at least your code lead me to some contourd surface.

I will come back and tell if it worked. Unfortunately I cannot disclose the data nor the results because of copyright issues.

So this mail is just to thank you for your responsiveness to my questions.

Timmie

Hello Jeff,
I just wanna give feedback on what got me going here:

#### data preparation
### data is loaded from a CSV file

···

###
lats = y # data[:,0]
## lon => x
lons = x # data[:,1]
## values => z
values = z #data[:,2]
###
lat_uniq = list(set(lats.tolist()))
nlats = len(lat_uniq)
lon_uniq = list(set(lons.tolist()))
nlons = len(lon_uniq)
color_map = plt.cm.spectral
print lats.shape, nlats, nlons
yre = lats.reshape(nlats,nlons)
xre = lons.reshape(nlats,nlons)
zre = values.reshape(nlats,nlons)
#### later in the defined map
CT = m.contourf(xre, yre, zre, cmap=color_map)

Of course, this can be simplified. But, really, in the end the solution was just simple and I hadn't thought of it...

If you want I can try to create a short example for the distribution.

Thanks for your help again,
Timmie