考虑这个y(x)函数:
![]()
我们可以在文件中生成这些分散的点:dataset_1D.dat:# x y
0 0
1 1
2 0
3 -9
4 -32
以下是这些点的一维插值代码:加载这些分散的点
创建x_mesh
执行1D插值
代码:
^{pr2}$
图中显示了以下内容:

现在,考虑这个z(x,y)函数:
![]()
我们可以在文件中生成这些分散的点:dataset_2D.dat:# x y z
0 0 0
1 1 0
2 2 -4
3 3 -18
4 4 -48
在这种情况下,我们必须执行二维插值:import numpy as np
from scipy.interpolate import interp1d, interp2d, interpnd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Load the data:
x, y, z = np.loadtxt('./dataset_2D.dat', skiprows = 1).T
# Create the function Z_inter for interpolation:
Z_inter = interp2d(x, y, z)
# Create the x_mesh and y_mesh :
x_mesh = np.linspace(1.0, 4, num=10)
y_mesh = np.linspace(1.0, 4, num=10)
print x_mesh
print y_mesh
# We calculate the z-interpolated of this x_mesh and y_mesh :
Z_interpolated = Z_inter(x_mesh, y_mesh)
print Z_interpolated
print type(Z_interpolated)
print Z_interpolated.shape
# plot:
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x, y, z, c='r', marker='o')
plt.legend(['data'], loc='lower left', prop={'size':12})
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
图中显示了以下内容:

其中,分散的数据再次以红点显示,以与二维图一致。在我不知道如何解释Z_interpolated结果:
根据上面代码的打印线,
Z_interpolated是一个n维numpy数组,形状为(10,10)。换句话说,是一个有10行10列的二维矩阵。
对于x_mesh[i]和{}的每个值,我都希望得到一个插值的z[i]值,为什么我没有收到这个?在我怎样才能在3D绘图中绘制插值数据(就像2D绘图中的黑色十字)?在