35

我想生成从 3D 数组中获得的线条。

这是代码:

VecStart_x = [0,1,3,5]
VecStart_y = [2,2,5,5]
VecStart_z = [0,1,1,5]
VecEnd_x = [1,2,-1,6]
VecEnd_y = [3,1,-2,7]
VecEnd_z  =[1,0,4,9]

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot([VecStart_x ,VecEnd_x],[VecStart_y,VecEnd_y],[VecStart_z,VecEnd_z])
plt.show()
Axes3D.plot()

我得到那个错误:

ValueError:第三个参数必须是格式字符串

4

2 回答 2

41

我想,你想绘制 4 行。那你可以试试

for i in range(4):
    ax.plot([VecStart_x[i], VecEnd_x[i]], [VecStart_y[i],VecEnd_y[i]],zs=[VecStart_z[i],VecEnd_z[i]])

正如@Nicolas 所建议的,请查看 matplotlib 库。

于 2012-07-18T12:40:48.060 回答
14

画廊是查找示例的绝佳起点:

http://matplotlib.org/gallery.html

这里有一个 3d 线图的例子:

http://matplotlib.org/examples/mplot3d/lines3d_demo.html

您会看到需要将 3 个向量传递给 ax.plot 函数。您实际上是在传递列表列表。我不知道您所说的 Start 和 End 子列表是什么意思,但以下行应该可以工作:

ax.plot(VecStart_x + VecEnd_x, VecStart_y + VecEnd_y, VecStart_z +VecEnd_z)

在这里,我对子列表(连接)求和,以便按轴只有一个列表。

于 2012-07-18T12:36:55.233 回答