# Interpolate 2D data

I generated a cartesian grid in Python using NumPy's linspace and meshgrid, and I obtained some data over this 2D grid from an unknown function. I want to get an approximation of the value of the function over some points inside the boundaries of the grid which are not part of it. I do not have some other unstructured grid, I just want to know the value in certain points. I assume I have to interpolate the data somehow, but I am very clueless on this and reading the documentation about the interpolate module of SciPy and some related components doesn't help. How can I find out this interpolated data?

Note: I know what I want to accomplish, but I have never done a task like this before and I have some problems formulating this question in terms of proper concepts and vocabulary. If I am not clear enough please help me improve my question.

I particularly like the bivariate spline class for what I think you are describing. You can use it to make a function (i.e. it is callable at any point) which interpolates the data using a spline. If you want just an interpolation then you simply set the kx and ky values to 1. If you want a smoother function then increasing the order of the spline (arguably 3 is a good choice) and you can even use a smoothing factor which I never do. The x and y values you would use are the ones that linspace gave you and the z value would be the function values.

You can find a good overview of methods and vocabulary on interpolation in two dimensions at http://en.wikipedia.org/wiki/Multivariate_interpolation

• Thank you Arnold! This is indeed very helpful. I am checking this and see if now I can solve the problem, or at least improve the question.
– user782
Commented Jun 15, 2012 at 20:03

There is a nice video made by Travis Oliphant where he discusses 2D interpolation using python: see the youtube video Python Interpolation 3 of 4: 2d interpolation with Rbf and interp2d

• Didn't know the video, thank you for the resource.
– user782
Commented Jun 16, 2012 at 17:44

Let's say you have a 2D grid with the X-axis running from ${0,1,...,i,...,M}$ and the Y-axis running from ${0,1,...,j,...,N}$. Each $i,j$ in a non-negative integer.

Your data over the grid can be viewed as a function of the grid locations $(i,j)$. In effect, the data $z = f(i,j)$.

Let's say you want the value of this function at $(i',j')$, where $i'=i+\delta{i}$ and $j'=j+\delta{j}$, such that $\delta{i}$ and $\delta{j}$ are the fractional decimal values between $0$ and $1$. Your problem is to find $z' = f(i',j') = f(i+\delta{i}, j+\delta{j})$.

There are several options for interpolating on such a grid. One of the simplest methods is the nearest neighbor interpolation. In this kind of interpolation, you simply assign to $(i',j')$, the value of the closest grid point. A naive way of doing this is to round $i'$ and $j'$ to the nearest integer.

A slightly better interpolation scheme would use a weighted combination of its closest neighbors that lie on the grid. For example, with linear interpolation, you would use the four closest grid points $(i,j)$, $(i+1,j)$, $(i, j+1)$ and $(i+1,j+1)$ to find the appropriate interpolate value at $(i',j')$.

For fast easy spline interpolation on a uniform grid in 1d 2d 3d and up, I recommend scipy.ndimage.map_coordinates; see the plot and example code under
multivariate-spline-interpolation-in-python-scipy on SO.
For smoothly-varying nonuniform grids, there's a helper class Intergrid .