2014-01-17 60 views
0

我有一個python程序,可以爲我計算角度並將它們輸出到列表中。在圓柱座標中繪製單位矢量的堆棧 - matplotlib

我想要做的是繪製一堆箭頭,這些箭頭是指向角度方向的單位矢量。所以我認爲圓柱座標是最好的,因爲它們只有一個角座標。

我試過pyplot.quiver,但我不認爲它可以在3D中做任何事情,並且3D線圖也不起作用。 (長度,高度,角度)轉換爲一對矢量(a,b,c),(長度* cos(角度),長度* sin(角度),高度)?

回答

1

如果您有一個角度列表,您可以使用numpy輕鬆計算與這些角度相關的向量。

import numpy as np 
import matplotlib.pyplot as plt 
angles = np.random.rand(100) 

length = 1. 
vectors_2d = np.vstack((length * np.cos(angles), length * np.sin(angles))).T 

for x, y in vectors_2d: 
    plt.plot([0, x], [0, y]) 
plt.show() 

enter image description here


如果你真的想在圓柱形,而不是極COORDS,然後

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
angles = np.random.rand(100) 

length = 1. 
heights = np.arange(len(angles)) 
vectors_3d = np.vstack((length * np.cos(angles), 
         length * np.sin(angles), 
         heights)).T 

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
for x, y, z in vectors_3d: 
    ax.plot([0, x], [0, y], zs=[z, z]) 
plt.show() 

enter image description here


編輯:我知道如何在地塊上使用pyplot.quiver來放置箭頭。不過,我不認爲mplot3dquiver搭配很好。也許像@tcaswell這樣的人可以幫忙解決問題。但在二維,你可以做

import numpy as np 
import matplotlib.pyplot as plt 

angles = np.random.rand(100) 
# Define coords for arrow tails (the origin) 
x0, y0 = np.zeros(100), np.zeros(100) 
# Define coords for arrow tips (cos/sin) 
x, y = np.cos(angles), np.sin(angles) 

# in case you want colored arrows 
colors = 'bgrcmyk' 
colors *= colors * (len(x0)/len(colors) + 1) 
plt.quiver(x0, y0, x, y, color=colors[:len(x0)], scale=1) #scale sets the length 
plt.show() 
+0

輝煌,非常感謝。你知道如何添加一個箭頭到行尾嗎? – user3087409