繁体   English   中英

3D 表面插值

[英]Interpolation for 3D surface

我的数据在大小为 21 x 30 的 ndarray 中; 它包含每个点的速度值。 我制作了一个 3D 曲面图来可视化它,但数据不是那么平滑。 为了对数据进行插值,以便获得平滑的峰值,我尝试了griddata函数,但它似乎不起作用。

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
vel = np.genfromtxt(r'velocity.txt')
x = np.arange(0, 21, 1)
y = np.arange(0, 30, 1)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X,Y = np.meshgrid(x, y)
surf = ax.plot_surface(x, y, vel, cmap="RdBu")
fig.set_size_inches(10, 10)
plt.show()

从我从问题中可以理解,您需要做的是网格插值。 可以使用来自 scipy here的 RegularGridInterpolator 来做到这一点。 只需制作更精细的网格,在该网格上插值,然后绘制。

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import RegularGridInterpolator

vel=np.random.random((21,30))
#grid old
x=np.arange(0,21,1)
y=np.arange(0,30,1)
grid_old=(x,y)

#grid new
# the limits of the interpolated x and y val have to be less than the original grid
x_new=np.arange(0.1,19.9,0.1)
y_new=np.arange(0.1,28.9,0.1)
grid_new = np.meshgrid(x_new, y_new)
grid_flattened = np.transpose(np.array([k.flatten() for k in grid_new]))

#Interpolation onto a finer grid
grid_interpol = RegularGridInterpolator(grid_old,vel,method='linear')
vel_interpol = grid_interpol(grid_flattened)

#Unflatten the interpolated velocities and store into a new variable.
index=0
vel_new=np.zeros((len(x_new),len(y_new)))
for i in  range(len(x_new)):
    for j in range(len(y_new)):
        vel_new[i,j] =vel_interpol[index]
        index+=1

fig=plt.figure()
ax=fig.add_subplot(111,projection='3d')
surf=ax.plot_surface(grid_new[0],grid_new[1],vel_new.T, cmap="RdBu") 
fig.set_size_inches(10,10) 
plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM