Matplotlib箭袋

2024-04-23 08:59:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着用matploblib和箭袋函数绘制一些箭头。但我想用一个数组分别选择每个箭头的长度。

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.quiverhttp://matplotlib.sourceforge.net/examples/pylab_examples/quiver_demo.html

在这些演示和文档中,您可以根据单位(x、y、宽度、高度、xy、英寸…)按比例更改比例,是否有方法为每个箭头定义比例?


Tags: 函数apihttpnetmatplotlibhtml绘制箭头
2条回答

如果你想知道两个向量的相对向量大小,比如x和y,你可以试试这个非线性比例:

L = np.sqrt(x**2 + y**2)
U = x / L 
V = y / L 
#If we just plotted U and V all the vectors would have the same length since 
#they've been normalized.

m = np.max(L)
alpha = .1 
#m is the largest vector, it will correspond to a vector with 
#magnitude alpha in the quiver plot

S=alpha /(1+np.log(m/L)) 

U1=S*U 

V1=S*V

plt.figure() 
Q = plt.quiver(x,y,U1,V1,scale = 1., units='width') 

qk = plt.quiverkey(Q, 0.9, 0.9, 2, r'$2 \frac{m}{s}$', 
labelpos ='E',coordinates='figure')

可以通过增大或减小alpha的值来更改向量大小的范围。

指定每个箭头的位置和向量的长度是箭袋图的过度说明。所以你需要做的是改变你正在绘制的数据。

如果向量场是U和V(与示例中的U和V相同),则可以通过以下方式对它们进行规格化:

N = numpy.sqrt(U**2+V**2)  # there may be a faster numpy "normalize" function
U2, V2 = U/N, V/N

然后,可以应用所需的任何比例因子数组:

U2 *= F
V2 *= F

相关问题 更多 >