[SciPy-user] How Do I Interpolate a Grid of Data

josef.pktd at gmail.com josef.pktd at gmail.com
Sun May 3 20:43:23 EDT 2009


On Sun, May 3, 2009 at 7:10 PM, Joseph Smidt <josephsmidt at gmail.com> wrote:
> Hello,
>
>         I am new to python and scipy.  Lets say I have a file called
> grid.txt that looks like this
> ( x, y, f(x,y)):
>
> 1   1   0.6
> 3   1   0.8
> 7   1   2.3
> 1   3   0.3
> 3   3   1.5
> 7   3  1.3
> 1   7   2.6
> 3   7   2.8
> 7   7   1.3
>
> How would I, using scipy, interpolate this so I get a value at any point on
> a new grid [0,10]x[0,10]?   IE,  I would like to take the above
> information and create some NXN array f so that I could say print
> f[1][2] or print f[9][10] say, and it would give me the interpolated
> value at that point.  Thanks.
>

scipy.interpolate.interp2d  is easy to use for interpolation,
I just tried, and for points outside of the knot points it looks like
they are assumed constant.

for extrapolation, I'm not sure whether or how well any of the
interpolation methods in scipy.interpolate or
scipy.ndimage.interpolation work,

but  scipy.interpolate.Rbf might be worth a try if the number of knot
points is not too large.

Otherwise, I would try to first estimate or fix the extreme points of
your grid [0,10]x[0,10], before using interp2d.

Josef



>>> from scipy import interpolate
>>> x = [0,1,2,0,2,0,1,2];  y = [0,0,0,3,3,7,7,7]
>>> z = (1+np.array(x))*(1+np.array(y))
>>> z
array([ 1,  2,  3,  4, 12,  8, 16, 24])
>>> ip = interpolate.interp2d(x,y,z)
>>> ip(3,7)
array([ 24.])
>>> ip(0,10)
array([ 8.])
>>> ip(1,10)
array([ 16.])
>>> xn=np.linspace(0,2,5)
>>> yn=np.linspace(0,7,5)
>>> zn = ip(xn,yn)



More information about the SciPy-User mailing list