2012-07-18 31 views
13

我是新的python。我想生成这些线,我从3D数组中获得。如何使用Matplotlib制作简单的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异常:第三个参数必须是一个格式字符串

我感谢你的帮助。

回答

15

我想,你想绘制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库。

+0

修复它。谢谢。 – 2012-07-18 12:45:13

5

画廊是一个很好的起点,找出例子:

http://matplotlib.org/gallery.html

这里有三维线图为例:

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

你看,你需要传递给ax.plot函数3个向量。 你实际上正在传递列表清单。 我不知道你在开始的意思和结束的子表,但下面的行应该工作:

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

这里我总结了子列表(串联),以只有一个由轴列表。