给出二维网格上的函数的值z。网格坐标称为x和y。
现在,我想插入数据,这样我就可以得到任何(x,y)的z值。
为确保我的示例清晰,我不想缩短我的代码:
import numpy as np
import pylab as pl
from scipy.interpolate import SmoothBivariateSpline
from scipy.interpolate import interp1d
def Func(x, y): # Define a test-function.
return y**2 # A parabola.
SparseGridX, SparseGridY = np.mgrid[0:1:5j, 0:1:5j] # Generate a sparsely meshed grid.
SparseGridZ = Func(SparseGridX, SparseGridY) # A function evaluated for np-arrays of arbitrary shape, again yields np-arrays of the same shape. Hence, this yields again an array.
pl.figure(num='Sparsely sampled function.')
pl.pcolor(SparseGridX, SparseGridY, SparseGridZ) # Plot the sparsely sampled function on the grid.
pl.colorbar();pl.title("Sparsely sampled function.")
FineListX = np.linspace(0,1,20) # Generate a fine-mesh list along x...
FineListY = np.linspace(0,1,20) # ... as well as along y...
FineGridX, FineGridY = np.mgrid[0:1:20j, 0:1:20j] # ... and the corresponding fine-mesh grid.
ListedSparseGridX = SparseGridX.flatten() # Attain the list of x-coordinates in the fine-mesh grid.
ListedSparseGridY = SparseGridY.flatten() # Attain the list of y-coordinates in the fine-mesh grid.
ListedSparseGridZ = Func(ListedSparseGridX,ListedSparseGridY) # This yields a list, in which an element is the result of Func for the corresponding combination of elements of FineListX and FineListY.
IntObj = SmoothBivariateSpline(ListedSparseGridX, ListedSparseGridY, ListedSparseGridZ, kx=1, ky=1) # This yields an interpolated object of the sparsely meshed points.
Interpolation = IntObj(FineListX,FineListX) # This evaluates the interpolated object at the points of the fine-mesh grid and returns the corresponding array.
pl.figure(num='Fine-meshed interpolation.')
pl.pcolor(FineGridX, FineGridY, Interpolation) # Plot the interpolation in a fine-mesh grid.
pl.colorbar();pl.title("Fine-meshed interpolation.")
IntObj1Dim = interp1d(SparseGridY[3], SparseGridZ[3]) # This yields a one-dimensional interpolated object. The index 3 is arbitrary.
Interpolation1Dim = IntObj1Dim(FineListY) # This evaluates the interpolated object along the fine-meshed y-coordinate.
pl.figure(num="Plot only along y-coordinate.")
pl.plot(SparseGridY[3],SparseGridZ[3], label="Points to interpolate", marker="o", ls="None") # Plot the points (along y) that were used for the interpolation.
pl.plot(FineListY,Interpolation[5], label="Interpolation via SmoothBivariateSpline") # Plot the interpolation (along y).
pl.plot(FineListY,Interpolation1Dim, label="Interpolation via interp1d") # Plot the one-dimensional interpolation.
pl.legend(loc="best")
pl.title("Plot only along y-coordinate.")
如您所见,interp1d提供了一个完全符合要点的分段线性函数。相比之下,SmoothBivariateSpline只是产生一条直线。我的目标是使用SmoothBivariateSpline生成插值,它也是与interp1d生成的一样的分段线性。
我尝试使用SmoothBivariateSpline的参数s
,但我没有成功。特别是,我试过s=0
。在这种情况下,插值应该完全符合数据点,但事实并非如此。
提前致谢!
答案 0 :(得分:0)
现在我发现LinearNDInterpolator在这里更有用。
ListedSparseGrid = np.asarray([(i,j) for i, j in zip(ListedSparseGridX,ListedSparseGridY)]) # Attain a list of all pairs of the elements of ListedSparseGridX and ListedSparseGridY.
IntObjLNDI = LinearNDInterpolator(ListedSparseGrid, ListedSparseGridZ) # This yields an interpolated object of the sparsely meshed points.
InterpolationLNDI = np.asarray([[IntObjLNDI(i,j) for j in FineListY] for i in FineListX]) # This evaluates the interpolated object at the points of the fine-mesh grid and returns the corresponding array.
pl.figure(num='Fine-meshed interpolation using LinearNDInterpolator.')
pl.pcolor(FineGridX, FineGridY, InterpolationLNDI) # Plot the interpolation in a fine-mesh grid.
pl.colorbar();pl.title("Fine-meshed interpolation using LinearNDInterpolator.")
并且1-D结果等于interp1d的结果,通过添加
可以看到pl.plot(FineListY,InterpolationLNDI[40], label="Interpolation via LinearNDInterpolator") # Plot the interpolation (along y).
到名为&#34的图;仅沿y坐标绘图。"上方。