Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python interpolate point value on 2D grid

I have a regular 2D X, Y and Z array and I have a point X0 and Y0 and I want to know the Z0 value in point (X0, Y0) on my grid.

I found that scipy have interpolate module but as I understand it interpolates 1D/2D arrays and returns 1D/2D array, but there is no method that returns only one value at one point.

For example:

#My grid data
X = [ [X11, X12, X13, ..., X1N], 
      [X21, X22, X23, ..., X2N],
          ....
      [XN1, XN2, XN3, ..., XNN]

Y = [ [Y11, Y12, Y13, ..., Y1N], 
      [Y21, Y22, Y23, ..., Y2N],
          ....
      [YN1, YN2, YN3, ..., YNN] ]

Z = [ [Z11, Z12, Z13, ..., Z1N], 
      [Z21, Z22, Z23, ..., Z2N],
          ....
      [ZN1, ZN2, ZN3, ..., ZNN] ]

#Point at which I want to know the value of the Z
X0, Y0 = ..., ...

#Now I want to call any function that'll return the value at point (X0, Y0), Z0 is float value, not array
Z0 = interpolation(X, Y, Z, X0, Y0)

As I understand the similar function is scipy.interpolate.interpn but it works only with 1D arrays and give out an error when I want to work with 2D data

like image 326
Greg Avatar asked Feb 28 '17 09:02

Greg


People also ask

How do you interpolate a 2D array in Python?

Interpolate over a 2-D grid. x, y and z are arrays of values used to approximate some function f: z = f(x, y) which returns a scalar value z. This class returns a function whose call method uses spline interpolation to find the value of new points.

What does Griddata do in Python?

interpolate. griddata() method is used to interpolate on a 2-Dimension grid.

How do you interpolate gridded data?

To interpolate using a single set of values, specify V as an array with the same size as the full grid of sample points. For example, if the sample points form a grid with size 100-by-100, you can specify the values with a matrix of the same size.


1 Answers

  • you can also use griddata :

    points = np.array( (X.flatten(), Y.flatten()) ).T
    values = Z.flatten()
    
    from scipy.interpolate import griddata
    Z0 = griddata( points, values, (X0,Y0) )
    
  • X0 and Y0 can be arrays or even a grid.

  • you can also choose the interpolation with method=
  • perhaps you can find a way to get ride of the flatten(), but it should work.

(https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html)

like image 161
N.G Avatar answered Oct 10 '22 21:10

N.G