2012-07-18 36 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) 

這裏我總結了子列表(串聯),以只有一個由軸列表。