2

我在 .dat 文件中有一个非常大的 X、Y、Z 坐标数据数据集(约 500 万行)。我可以将其导入为 3D 散点图,但想在点之间的 3D 表面网格处进行插值。我已经查看了一些示例,包括this,但是如果没有出现 Invalid Index 错误,它就无法正常工作。

这是我所拥有的:

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]
4

1 回答 1

1

很确定问题是您调用griddata错误(文档)。尝试:

Z = scipy.interpolate.griddata((xs, ys), zs, X, Y)
于 2013-03-28T14:12:42.183 回答