如何关闭matplotlib quiver的缩放?

3
matplotlib.pyplot.quiver 函数接受一组“起点”和一组“终点”,然后绘制一堆箭头,从“起点”处开始,在“终点”的方向上指向。但是,有一个缩放因子,使箭头不一定在“终点”处结束,它们只是朝着那个方向指向。例如:
import matplotlib.pyplot as plt
import numpy as np

pts = np.array([[1, 2], [3, 4]])
end_pts = np.array([[2, 4], [6, 8]])

plt.quiver(pts[:,0], pts[:,1], end_pts[:,0], end_pts[:,1])

the_picture

请注意左下方的向量起点是 (1,2)(这正是我想要的),但它并没有以 (2,4) 结束。这是由于 quiver 函数中的 scale 参数控制箭头的长度。我该如何让箭头恰好结束于 (2,4)?
1个回答

6
quiver文档指出,如果要在x-y平面上绘制向量,其中u和v具有与x和y相同的单位,则使用angles='xy', scale_units='xy', scale=1。但请注意,uv是相对于位置理解的。因此,您需要先取差。
import matplotlib.pyplot as plt
import numpy as np

pts = np.array([[1, 2], [3, 4]])
end_pts = np.array([[2, 4], [6, 8]])
diff = end_pts - pts

plt.quiver(pts[:,0], pts[:,1], diff[:,0], diff[:,1],
           angles='xy', scale_units='xy', scale=1.)

plt.show()

enter image description here


2
谢天谢地!这应该是默认行为!如果在轴上绘制箭头,那么人们希望这些箭头实际上会注意到轴... - Atcold

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接