I was wondering if there is a function to interpolate in 3D space. Say for example i have a matrix with x,y,z coordinates. Is there a way i can interpolate and calculate the z coordinate for a given x and y. For example:
A=[1,3,5,
2,3,4
4,4,8
3,4,5]
And i need to calculate the z coordinate when x=3 y=3,5
Yes, scipy.interpolate
provides functions for interpolation on precomputed grids as well as objects that expose __call(x)__
function to compute values on demand
Consider the following example
import numpy as np
from scipy import interpolate
A=[1,3,5,
2,3,4,
4,4,8,
3,4,5]
xs = A[0::3]
ys = A[1::3]
zs = A[2::3]
points=np.zeros((len(xs),2))
values=np.zeros((len(xs)))
points[:,0] = xs
points[:,1] = ys
values[:] = zs
interpolator = interpolate.CloughTocher2DInterpolator(points,values)
z = interpolator(3,3.5)
print(z)
This code should output the value
5.489419430226453
It first converts the input list of n*3 values into three numpy arrays xs, ys, and zs. And then creates the interpolator object using the points
and values
arrays created at the previous stage.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.