SciPy interp2D for coordinate pairs


I'm using scipy.interpolate.interp2d to create an interpolation function for a surface. I then have two arrays of real data that I want to calculate interpolated points for. If I pass the two arrays to the interp2d function I get an array of all the points, not just the pairs of points.

My solution to this is to zip the two arrays into a list of coordinate pairs and pass this to the interpolation function in a loop:

f_interp = interpolate.interp2d(X_table, Y_table,Z_table, kind='cubic')

co_ords = zip(X,Y)
out = []
for i in range(len(co_ords)):
    X = co_ords[i][0]
    Y = co_ords[i][1]
    value = f_interp(X,Y)

My question is, is there a better (more elegant, Pythonic?) way of achieving the same result?

For one, you can do

for Xtmp,Ytmp in zip(X,Y):

in your loop. Or even better, just

out = [float(f_interp(XX,YY)) for XX,YY in zip(X,Y)]

replacing the loop.

On a different note, I suggest using interpolate.griddata instead. It tends to behave much better than interp2d, and it accepts arbitrary-shaped points as input. As you've seen, interp2d interpolators will only return you values on a mesh.