为axes3d创建表面数据

use*_*130 5 python matplotlib

好吧,我对这个问题表示歉意,但我要在这里拉扯我的头发。

我在 python 中加载了一个数据结构,格式如下:

[(1,0,#),(1,1,#),(1,2,#),(1,3,#),(2,0,#),(2,1,#) ... (26,3,#)]
Run Code Online (Sandbox Code Playgroud)

每次我希望在 z 轴上表示时, # 都是不同的数字。您可以看到 x 和 y 始终是整数。

绘制散点图很简单:

x,y,z = zip(*data)
fig = plt.figure()
ax = fig.gca(projection = '3d')
surface = ax.scatter(x, y, z)
plt.show()
Run Code Online (Sandbox Code Playgroud)

但说到表面,我可以看到两种方法:

1) Call ax.plot_trisurf(),它应该与一维数组一起工作,类似于ax.scatter()并且显然可以在这里工作,但对我来说给了我一个错误:

"AttributeError: Axes3D subplot object has not attribute 'plot_trisurf'"
Run Code Online (Sandbox Code Playgroud)

如果我使用以下位置的示例源代码,也会出现此错误: http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#tri-surface-plots,表明我的安装有问题 - 我的 Matplotlib 版本是 1.1.1rc ,. 例如,如果ax.plot_surface()调用 或 ,则不会出现此错误ax.scatter()

2)使用meshgrid()griddata()结合ax.plot_surface()- 无论哪种情况,在仔细阅读文档和示例两天之后,我仍然不明白如何在我的情况下正确使用它们,特别是在生成 Z 值时。

任何帮助将非常感激。

dan*_*van 4

为了解决您的第一个问题(1),我相信您需要Axes3Dmplot3d库中导入,即使您没有直接调用它。也许尝试添加

from mpl_toolkits.mplot3d import Axes3D
Run Code Online (Sandbox Code Playgroud)

在你的主代码之前(这一行在阅读教程时触发了记忆)。

至于(2)、XYZ需要是矩阵(二维数组)类型对象。这可能会让人感到困惑,但你可以考虑一个例子:

# two arrays - one for each axis
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)

# create a mesh / matrix like object from the arrays
X, Y = np.meshgrid(x, y)
# create Z values - also in a mesh like shape
Z = np.sin(np.sqrt(X**2 + Y**2))

# plot!
surface = ax.plot_surface(X, Y, Z)
Run Code Online (Sandbox Code Playgroud)