gpt4 book ai didi

python - 是否有 python 函数/库可以从网格数据创建字段?

转载 作者:太空宇宙 更新时间:2023-11-03 17:26:44 25 4
gpt4 key购买 nike

我有在 3D 网格上指定的数据,例如:

import numpy as np
f = np.random.rand(4, 4, 4)

有没有办法获得该数据的任意点(即不在网格上)的(插值)值? interpolation文档显示您可以显示此类数据,但我不知道如何获取 f(任意点) 的。谢谢。

最佳答案

scipy.interpolate.griddata允许 N 维插值。下面是一个 3D 示例,其中我在网格上计算了 f(r) = cos(r)^2,然后对网格数据进行插值。可以在下面绘制网格数据或插值数据,以验证插值是否正确工作:

%matplotlib inline
from __future__ import division
import numpy as np
from scipy import interpolate
from math import *
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

#define grid points and f(r) = cos^2(r) and format for interpolation:
N = 10;
x_range, y_range, z_range = [np.linspace(0,pi,N), np.linspace(0,pi,N), np.linspace(0,pi,N)];
x_grid,y_grid,z_grid = np.meshgrid(x_range,y_range,z_range);
xi, yi, zi = x_grid.ravel(), y_grid.ravel(), z_grid.ravel();
xi, yi, zi = xi[:], yi[:], zi[:];
f = np.cos((xi**2 + yi**2 + zi**2)**(1/2))**2;
points = np.dstack([xi, yi, zi]);
points = points[0,:,:];

#create random points and interpolate f(r):
[x_rand, y_rand, z_rand] = [pi*np.random.rand(N**3), pi*np.random.rand(N**3), pi*np.random.rand(N**3)]
random_points = np.dstack([x_rand, y_rand, z_rand])
random_points = random_points[0,:,:]

f_interpolated = interpolate.griddata(points, f, random_points, method='linear');

#PLOT THE RESULTS:
fig = plt.figure()
ax = fig.gca(projection='3d')

#_____________PLOT GRID OR INTERPOLATED DATA ______________
surf = ax.scatter(xi,yi,zi,c=f,s=20,alpha=.8)
surf = ax.scatter(x_rand,y_rand,z_rand,c=f_interpolated)

fig.colorbar(surf, aspect=16)

plt.show()

关于python - 是否有 python 函数/库可以从网格数据创建字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32448612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com