matplotlib-users

 [Matplotlib-users] Plotting grid data From: Mauro Cavalcanti - 2008-12-09 11:59:10 Attachments: wg2.gif ```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, -- Dr. Mauro J. Cavalcanti Ecoinformatics Studio P.O. Box 46521, CEP 20551-970 Rio de Janeiro, RJ, BRASIL E-mail: maurobio@... Web: http://studio.infobio.net Linux Registered User #473524 * Ubuntu User #22717 "Life is complex. It consists of real and imaginary parts." ```
 Re: [Matplotlib-users] Plotting grid data From: Jeff Whitaker - 2008-12-09 12:26:40 ```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@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > -- Jeffrey S. Whitaker Phone : (303)497-6313 NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449 325 Broadway Boulder, CO, USA 80305-3328 ```
 Re: [Matplotlib-users] Plotting grid data From: Mauro Cavalcanti - 2008-12-09 13:05:00 ```Dear Jeff, 2008/12/9 Jeff Whitaker : > 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@... Web: http://studio.infobio.net Linux Registered User #473524 * Ubuntu User #22717 "Life is complex. It consists of real and imaginary parts." ```
 Re: [Matplotlib-users] Plotting grid data From: Jeff Whitaker - 2008-12-09 13:17:56 ```Mauro Cavalcanti wrote: > Dear Jeff, > > 2008/12/9 Jeff Whitaker : > >> 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 ```
 Re: [Matplotlib-users] Plotting grid data From: Jeff Whitaker - 2008-12-09 15:08:07 ```Mauro Cavalcanti wrote: > Dear Jeff, > > 2008/12/9 Jeff Whitaker : > >> Mauro: The shape of x, y, and dat must all be (nlats, nlons). >> > > I tried to do: > > coords = numpy.loadtxt('grid.dat') > lon = coords[:,0] > lat = coords[:,1] > dat = coords[:,2] > X, Y = numpy.meshgrid(lon, lat) > nlons = X.shape[1] > nlats = X.shape[0] > Z = dat.reshape(nlats,nlons) > > (so that I could proceed to plotting with the X,Y,Z arrays), but got > another error: > > "ValueError: total size of new array must be unchanged" > > and with this I get lost.... :( > > I tried to follow the guidelines you provided for Mr. Tim Michelsen on > a previous post > (http://www.nabble.com/plotting-a-contour-map-from-CSV-file-td18440459.html) > but it didn't help (however, that provided me with a solution to > another problem!). > > Best regards, > > Mauro: This will only work is dat is length(nlons*nlats). Is it? -Jeff -- Jeffrey S. Whitaker Phone : (303)497-6313 NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449 325 Broadway Boulder, CO, USA 80305-3328 ```
 Re: [Matplotlib-users] Plotting grid data From: Scott Sinclair - 2008-12-09 16:33:39 Attachments: temp.py ```> Mauro Cavalcanti wrote: > I tried to do: > > coords = numpy.loadtxt('grid.dat') > lon = coords[:,0] > lat = coords[:,1] > dat = coords[:,2] > X, Y = numpy.meshgrid(lon, lat) > nlons = X.shape[1] > nlats = X.shape[0] > Z = dat.reshape(nlats,nlons) > > (so that I could proceed to plotting with the X,Y,Z arrays), but got > another error: > > "ValueError: total size of new array must be unchanged" > > and with this I get lost.... :( Hi Mauro, As Jeff noted earlier, the number of elements in an array can't be increased simply by reshaping it, so the error message isn't too mysterious, if carefully considered. This might be a good time to step back from the code for a moment and consider the problem that needs to be solved. As I understand it: 1. You have a small number of scattered data values in 'grid.dat' with their locations specified by lat/lon co-ordinates. 2. You want to create a regular square/rectangular grid that spans your region of interest and assign the values in 'grid.dat' to the grid box in which they fall, with all other regions of the grid being masked. 3. You then want to plot the masked rectangular grid onto a Basemap instance. The attached script shows one way to do this (using the imshow method), I've left the task assigning the data values to the correct grid box for you.. Cheers, Scott ```
 Re: [Matplotlib-users] Plotting grid data From: Mauro Cavalcanti - 2008-12-09 17:12:25 ```Dear Scott & Jeff, Sorry for the delay in replying, I was off office for a while. 2008/12/9 Scott Sinclair : > As Jeff noted earlier, the number of elements in an array can't be > increased simply by reshaping it, so the error message isn't too > mysterious, if carefully considered. Sure, I got lost because I tried to follow the solution previously presented in the posting I referred to, since the example data I provided are identical in format to that used in that solution (a 3-column space delimited file, with the first two representing the longitude and latitude, and the 3rd representing the variable of interest). > 1. You have a small number of scattered data values in 'grid.dat' with > their locations specified by lat/lon co-ordinates. > 2. You want to create a regular square/rectangular grid that spans > your region of interest and assign the values in 'grid.dat' to the > grid box in which they fall, with all other regions of the grid being > masked. > 3. You then want to plot the masked rectangular grid onto a Basemap instance. You did understood it very correctly, and of course offered a much better description than I could provide myself using my pidgin English :-). Indeed, there is just one detail to add to the above problem description, and this is the fact that the "region of interest" may span the entire world (land or sea, depending on the biological organism under study). > The attached script shows one way to do this (using the imshow > method), I've left the task assigning the data values to the correct > grid box for you.. Thank you very much, I feel somewhat ashamed! Anyway, the final product of this project will be made freely available under GPL, so it will belong to everyone (and due credit will be given to all of you meteorologists, physicists, mathematicians, etc. that have been so helpful!) Returning to the techical aspects of the problem, it is indeed the assignment of data values to the correct grid box that lays in the core of the second part of my question -- it may indeed be not so difficult as the previous requirement, that is to build the data file (with the data values of interest) *before* the grid analysis, from simple lat-long files for each species under study (usually up to 20, but may be more). But I will develop this further. One more detail: is it possible to plot the value of the variable of interest, corresponding to each square of the grid, inside the respective square? With warmest regards, -- Dr. Mauro J. Cavalcanti Ecoinformatics Studio P.O. Box 46521, CEP 20551-970 Rio de Janeiro, RJ, BRASIL E-mail: maurobio@... Web: http://studio.infobio.net Linux Registered User #473524 * Ubuntu User #22717 "Life is complex. It consists of real and imaginary parts." ```