# [SciPy-user] 2d interpolation

Nils Wagner nwagner@iam.uni-stuttgart...
Thu Aug 2 01:54:09 CDT 2007

Michael Hearne wrote:
> All:  I'm trying to use interp2d to replicate behavior in Matlab.
>
> The Matlab script:
> x = reshape(1:16,4,4)';
> xi = 1:0.5:4;
> yi = [1:0.5:4]';
> z = interp2(x,xi,yi,'linear')
>
> which results in the matrix:
> z =
>
>     1.0000    1.5000    2.0000    2.5000    3.0000    3.5000    4.0000
>     3.0000    3.5000    4.0000    4.5000    5.0000    5.5000    6.0000
>     5.0000    5.5000    6.0000    6.5000    7.0000    7.5000    8.0000
>     7.0000    7.5000    8.0000    8.5000    9.0000    9.5000   10.0000
>     9.0000    9.5000   10.0000   10.5000   11.0000   11.5000   12.0000
>    11.0000   11.5000   12.0000   12.5000   13.0000   13.5000   14.0000
>    13.0000   13.5000   14.0000   14.5000   15.0000   15.5000   16.0000
>
> I had thought the following Python/numpy script would be equivalent,
> but it is not:
> from scipy.interpolate import interpolate
> from numpy.random import randn
> from numpy import *
>
> data = arange(16)
> data = data+1
> data = data.reshape(4,4)
> xrange = arange(4)
> yrange = arange(4)
> X,Y = meshgrid(xrange,yrange)
>
> outgrid = interpolate.interp2d(X,Y,data,kind='linear')
> xi = array([0,0.5,1,1.5,2,2.5,3])
> yi = xi
>
> z = outgrid(xi,yi)
>
> This results in the matrix:
> [[  1.           1.10731213   2.           2.89268787   3.
> 3.25045605
>     4.        ]
> [  3.           2.57118448   4.           5.42881552   5.
> 4.90975947
>     6.        ]
> [  5.           4.03505682   6.           7.96494318   7.
> 6.56906289
>     8.        ]
> [  7.           5.49892917   8.          10.50107083   9.
> 8.22836631
>    10.        ]
> [  9.           6.96280152  10.          13.03719848  11.
> 9.88766973
>    12.        ]
> [ 11.           8.42667386  12.          15.57332614  13.
> 11.54697315
>    14.        ]
> [ 13.           9.89054621  14.          18.10945379  15.
> 13.20627657
>    16.        ]]
>
> (Incidentally, is there a way to pretty-print arrays in numpy?  The
> above is kind of ugly and hard to read)
>
You may use set_printoptions
Help on function set_printoptions in module numpy.core.arrayprint:

set_printoptions(precision=None, threshold=None, edgeitems=None,
linewidth=None,
suppress=None, nanstr=None, infstr=None)
Set options associated with printing.

:Parameters:
precision : int
Number of digits of precision for floating point output
(default 8).
threshold : int
Total number of array elements which trigger summarization
rather than full repr (default 1000).
edgeitems : int
Number of array items in summary at beginning and end of
each dimension (default 3).
linewidth : int
The number of characters per line for the purpose of inserting
line breaks (default 75).
suppress : bool
Whether or not suppress printing of small floating point values
using scientific notation (default False).
nanstr : string
String representation of floating point not-a-number
(default nan).
infstr : string
String representation of floating point infinity (default inf).

e.g.
set_printoptions(precision=4,linewidth=120)