在圆柱坐标中绘制单位矢量的堆栈 - matplotlib

问题描述:

我有一个python程序,可以为我计算角度并将它们输出到列表中。在圆柱坐标中绘制单位矢量的堆栈 - matplotlib

我想要做的是绘制一堆箭头,这些箭头是指向角度方向的单位矢量。所以我认为圆柱坐标是最好的,因为它们只有一个角坐标。

我试过pyplot.quiver,但我不认为它可以在3D中做任何事情,并且3D线图也不起作用。 (长度,高度,角度)转换为一对矢量(a,b,c),(长度* cos(角度),长度* sin(角度),高度)?

如果您有一个角度列表,您可以使用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