Search code examples
pythonarraysscipyinterpolation

Why does scipy's interp2d return a 2D array instead of an 1D list?


I am trying to interpolate values on a 2D field that looks e.g. like this:

Example of 2D field

where the inner field represents a value of 1.

If I use an interp2d interpolator like this:

# x: 1D list of x values, length n
# y: 1D list of y values, length m
# z: 2D array of z values, dimensions nxm
interpolator = interpolate.interp2d(x=x, y=y, z=z)

Now when I call the interpolator with interpolator([0.2, 0.4, 0.6], [0, 0, 0]) I would expect an 1D list of length 3 filled with ones. However I get a 3x3 array filled with ones. In the example only the first row of the results is actually used (z[0, :]). But what do the other rows of the result represent then?


Solution

  • Based on what you've specified: z = f(x,y) is of dimensions n*m. So, interpolator([0.2, 0.4, 0.6], [0, 0, 0]) returning a 3x3 array makes sense.

    In the Scipy example, they are plotting only the first row. Dimensionally,

    x, y, xnew, ynew -> 1D

    z, xx, yy, znew -> 2D