gpt4 book ai didi

python - 从 X、Y、Z 坐标数据创建 3D 表面网格

转载 作者:太空宇宙 更新时间:2023-11-04 06:16:01 28 4
gpt4 key购买 nike

我在 .dat 文件中有一个非常大的 X、Y、Z 坐标数据集(约 500 万行)。我可以将其导入并绘制为 3D 散点图,但我想在点之间的 3D 表面网格处进行插值。我看过一些例子,包括 this , 但如果不收到无效索引错误就无法让它工作。

这是我所拥有的:

from pylab import *
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata

x, y, z = loadtxt('test.dat', unpack = True)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

xs = x
ys = y
zs = z


fig = plt.figure()
ax = fig.gca(projection='3d')

xi = np.linspace(min(xs), max(xs))
yi = np.linspace(min(ys), max(ys))

X, Y = np.meshgrid(xi, yi)
Z = griddata(xs, ys, zs, xi, yi)

surf = ax.plot_surface(X, Y, Z, rstride=5, cstride=5, cmap=cm.jet,
linewidth=1, antialiased=True)

ax.set_zlim3d(np.min(Z), np.max(Z))
fig.colorbar(surf)

plt.show()

这是我得到的错误:

python test2.py
Traceback (most recent call last):
File "test2.py", line 25, in <module>
Z = griddata(xs, ys, zs, xi, yi)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/mlab.py", line 2768, in griddata
tri = delaunay.Triangulation(x,y)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.py", line 90, in __init__
self.hull = self._compute_convex_hull()
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.py", line 115, in _compute_convex_hull
edges.update(dict(zip(self.triangle_nodes[border[:,0]][:,1],
IndexError: invalid index

我是 python 的新手,无法理解这一点。有任何想法吗?谢谢!

这是我用作测试的部分数据集:

x = [-0.91392505 -0.89382458 -0.87372404 -0.85362357 -0.83352304 -0.81342256
-0.79332203 -0.77322155 -0.75312102 -0.73302054 -0.71292001 -0.69281954
-0.672719 -0.65261853 -0.63251805 -0.61241752 -0.59231698 -0.57221651
-0.55211604 -0.5320155 ]
y = [ 111.25222 111.25222 111.25222 111.25222 111.25222 111.25222
111.25222 111.25222 111.25222 111.25222 111.25222 111.25222
111.25222 111.25222 111.25222 111.25222 111.25222 111.25222
111.25222 111.25222]
z = [ 1.42150589e-06 7.77236906e-08 3.33243515e-05 1.70491203e-05
6.01433214e-08 9.20339880e-06 4.21266122e-06 3.39080143e-04
1.37568408e-04 7.34613104e-06 1.07246015e-05 3.39363214e-06
1.09533859e-04 6.55860058e-05 2.83635192e-04 1.87549119e-06
9.96281233e-06 2.78222607e-04 2.88286283e-05 4.60408737e-05]

最佳答案

很确定问题是您错误地调用了 griddata ( documentation )。尝试:

Z = scipy.interpolate.griddata((xs, ys), zs, X, Y)

关于python - 从 X、Y、Z 坐标数据创建 3D 表面网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15668454/

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