Matplotlib箭头图缩放
我正在尝试使用matplotlib中的quiver函数绘制一些箭头。不过,我想单独为每个箭头选择长度,想用一个数组来实现。
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.quiver http://matplotlib.sourceforge.net/examples/pylab_examples/quiver_demo.html
在这些示例和文档中,展示了你可以根据单位(比如x、y、宽度、高度、xy、英寸等)来按比例改变比例尺。那么,有没有办法为每个箭头定义一个单独的比例尺呢?
2 个回答
0
如果你想了解两个向量,比如说 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 的值来改变向量大小的范围,增大或减小这个值就能影响结果。
15
要指定每个箭头的位置、方向和长度,这样做对箭头图来说就有点过于详细了。所以你需要做的是改变你要绘制的数据。
如果你有向量场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