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?