如何显示使用 quiver 创建的箭头的图例?

3
如何在使用pyplot.quiverpyplt.annotate方法绘制箭头时放置图例?
我目前正在使用以下代码:
import matplotlib.pyplot as plt
from numpy.random import *


x, y  = [1, 2, 3], [0.5, 0.5, 0.5]
u1,v1 = randn(3), randn(3)
u2,v2 = randn(3), randn(3)
u3,v3 = randn(3), randn(3)
QV1 = plt.quiver(x, y, u1, v1, color='r')
QV2 = plt.quiver(x, y, u2, v2, color='b')
QV3 = plt.quiver(x, y, u3, v3, color='g')

我希望能够放置一个简单的图例,以一种简单的方式显示三个不同颜色的箭头。
plt.plot(x, y, color='r', label='red')
...
plt.legend()
2个回答

2
您可以使用 quiverkey 函数来制作箭头图例。
plt.quiverkey(QV1, 1.2, 0.515, 2, 'arrow 1', coordinates='data')
plt.quiverkey(QV2, 1.2, 0.520, 2, 'arrow 2', coordinates='data')
plt.quiverkey(QV3, 1.2, 0.525, 2, 'arrow 3', coordinates='data')

2
好的,但是我们必须预先知道x、y的值。如何使用plt.legend()自动设置这个? - MeadowMuffins
1
我也想知道。我只发现写引用值,例如“qk = ax1.quiverkey(Q, 0.9, 0.9, 2, r'$2 \frac{m}{s}$', labelpos='E', coordinates='figure')”。 - smi

0
我的解决方案是绘制一个空的散点图,并使用latex箭头作为标记。
[...]

plt.quiver(x, y, u2, v2, color='b')
plt.scatter([], [], marker=r'$\longrightarrow$', c="black", s=120, label="(x,y) -> (u2,v2)")

这看起来不太好,但事实就是这样 :(

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