Pregunta

Having received a very helpful and useful solution to my problem of contouring irregular data,Contours with map overlay on irregular grid in python, here I still have the problem of combining the contours with the basemap. For this I know I have to do: x,y = m(lon,lat). None of the SO posts I have seen corresponds to my needs. My problem is where do I get (from my various parameters available) the x,y that the lon, lat use in the above formula? Here is my map object:

m = Basemap(projection = 'merc',llcrnrlon = 21, llcrnrlat = -18, urcrnrlon = 34, urcrnrlat = -8, resolution='h')

and here is the data for contours and the gridding:

data = pd.read_csv('meansr.txt', delim_whitespace=True)
numcols, numrows = 300, 300
xi = np.linspace(data.Lon.min(), data.Lon.max(), numcols)
yi = np.linspace(data.Lat.min(), data.Lat.max(), numrows)
xi, yi = np.meshgrid(xi, yi)

x, y, z = data.Lon.values, data.Lat.values, data.Z.values
zi = griddata(x, y, z, xi, yi)

Here are the plotting commands

plt.figure()
plt.contourf(xi, yi, zi)
plt.scatter(data.Lon, data.Lat, c=data.Z, s=100,
vmin=zi.min(), vmax=zi.max())
plt.colorbar()
plt.show()

This gives me two plots, a basemap and contours side by side, which I think is due to the difference in the two coordinates. Pointing inside the contours indicates the correct latitudes and longitudes, while on the map the x, y displayed are very large, order of 2000 and above. Any help please.

¿Fue útil?

Solución

Changed my reply a bit, so that what i typed in reply to your changed version of your problem: https://stackoverflow.com/a/20950887/2393569 works with the same names/... as in your code you gave here:

data = pd.read_csv('meansr.txt', delim_whitespace=True) 
numcols, numrows = 30, 30 
xi = np.linspace(data.Lon.min(), data.Lon.max(), numrows)
yi = np.linspace(data.Lat.min(), data.Lat.max(), numcols)
xi, yi = np.meshgrid(xi, yi)
x, y, z = data.Lon, data.Lat, data.Z

# NOTE: from here on it changes
points = np.vstack((x,y)).T 
values = z
wanted = (xi, yi)
zi = griddata(points, values, wanted) # for more info on griddata: http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html

# from here its the same code again as you had
plt.figure()
plt.contourf(xi, yi, zi)
plt.scatter(data.Lon, data.Lat, c=data.Z, s=100, vmin=zi.min(), vmax=zi.max())
plt.colorbar()
plt.show()

so the problem you actually had was not fully understanding scipy.interpolate.griddata, and not really a problem related to matplotlib.

So the correct use is

scipy.interpolate.griddata(points, values, xi, method='linear', fill_value=nan)
# 'points' has to contain what you named  x and y
# 'values' has to contain what you named z
# 'xi' has to contain what you named xi,yi

Does the plotting still give you trouble after you change this?

Note: i assume your imports are:

import numpy as np
from scipy.interpolate import griddata
import pylab as plt
# i guess the pd.read_csv is something pandas (heard of it, but never used it before, but since the only thing you do is read_csv i guess that wont cause problems)

It is best to give your imports too, if you ask a question here, since i just found out that from matplotlib.mlab import griddata exists too, and with a similar syntax to what you used, in Contours with map overlay on irregular grid in python you use the griddata from scipy, but the answer, on which you seem to have based your follow-up question (this one) contains the matplotlib.mlab import of griddata (which works differently). Does this solve your confusion?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top