从X,Y,Z坐标数据创建3D曲面网格

时间:2013-03-27 20:10:17

标签: python matplotlib

我在.dat文件中有一个非常大的数据集(约500万行)的X,Y,Z坐标数据。我可以将它作为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]

1 个答案:

答案 0 :(得分:1)

非常确定问题是你在调用griddata错误(documentation)。尝试:

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