未找到带有可放入图例的标签的手柄 [英] No handles with labels found to put in legend
问题描述
我正在尝试在PyPlot中创建一个平行四边形.我不愿使用以下代码绘制平行四边形-首先我将矢量箭头放入其中:
I'm trying to create a parallelogram in PyPlot. I'm not up to drawing the parallelogram--first I'm putting in the vector arrows--using the following code:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
plt.axis([-5,5,-5,5])
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.grid()
plt.arrow(0,0, 3,1, head_width=0.2, color='r', length_includes_head=True, label='u')
plt.arrow(0,0, 1,3, head_width=0.2, color='r', length_includes_head=True, label='v')
plt.arrow(0,0, 4,4, head_width=0.2, color='r', length_includes_head=True, label='u+v')
plt.legend()
这将返回以下错误:
No handles with labels found to put in legend.
我不知道为什么,因为根据 plt.arrow()
的文档,label
是可接受的 kwarg,而 plt.legend()
表面上应该在阅读.图的其余部分很好.它只是缺少传说.
I'm not sure why, because, based on the documentation for plt.arrow()
, label
is an acceptable kwarg, and plt.legend()
should ostensibly be reading that. The rest of the figure draws fine; it's just missing the legend.
推荐答案
可能已经很晚了,但对于有相同问题的任何人,解决方案都将方法 legend()
用于相应的斧头
不同于 plt
It might be late but for anyone with the same issue the solution is using the method legend()
for the corresponding ax
not as for plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
plt.axis([-5,5,-5,5])
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.grid()
plt.arrow(0,0, 3,1, head_width=0.2, color='r', length_includes_head=True, label='u')
plt.arrow(0,0, 1,3, head_width=0.2, color='r', length_includes_head=True, label='v')
plt.arrow(0,0, 4,4, head_width=0.2, color='r', length_includes_head=True, label='u+v')
ax.legend()
这篇关于未找到带有可放入图例的标签的手柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!