【问题标题】:No handles with labels found to put in legend没有发现带有标签的把手放在图例中
【发布时间】:2020-04-08 07:58:56
【问题描述】:

我正在尝试在 PyPlot 中创建平行四边形。我不擅长绘制平行四边形——首先我要放入矢量箭头——使用以下代码:

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() 表面上应该正在阅读它。图的其余部分画得很好;它只是缺少传说。

【问题讨论】:

    标签: python matplotlib legend


    【解决方案1】:

    使用以下划线开头的标签时出现此错误

    plt.plot(x, y, label = '_bad_name')

    从标签中删除前下划线解决了这个问题

    【讨论】:

      【解决方案2】:

      我面临类似的问题,例如找不到带有标签的句柄以放入图例中。

      首先我的代码是这样的

      figure, axis = pyplot.subplots(nrows=1,ncols=2, figsize=(15, 6), tight_layout=True)
      
      axis[0].legend(title='Country', title_fontsize = 12) #this line
      axis[0].pie(x=piechart_result['value_eur'],labels=piechart_result['short_name'])
      
      axis[1].pie(x=piechart_result['value_eur'],labels=piechart_result['short_name')
      
      pyplot.show()
      

      然后我改成

      figure, axis = pyplot.subplots(nrows=1,ncols=2, figsize=(15, 6), tight_layout=True)
      
      axis[0].pie(x=piechart_result['value_eur'],labels=piechart_result['short_name'])
      axis[0].legend(title='Country', title_fontsize = 12) # this line 
      
      axis[1].pie(x=piechart_result['value_eur'],labels=piechart_result['short_name')
      
      pyplot.show()
      

      在 colab 笔记本中为我工作

      【讨论】:

        【解决方案3】:

        错误是因为你没有指定标签文本

        要么做这样的事情

        plt.hist([x01, x02,x03], color=["lightcoral","lightskyblue","slategrey"], stacked=True, 
                     label=['Supressed','Active','Resolved'])
        plt.legend()
        

        或者

        不要使用 plt.legend()如果您没有指定标签文本,如以下错误示例

        plt.hist([x01])
        plt.legend()
        

        上面会抛出同样的错误,所以要么删除图例函数,要么提供它需要的 -> 标签。 旁注:这里 x01 只是我正在为其创建直方图的数字列表,在第一个示例中,它们是用于创建堆叠条形图的三个数字列表

        底线是由于未指定图例文本和调用/初始化图例而引发此错误

        【讨论】:

        • 每个箭头都有一个指定的标签。
        【解决方案4】:

        可能为时已晚,但对于遇到相同问题的任何人,解决方案是使用 legend() 方法处理相应的 ax 而不是 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()
        

        【讨论】:

        • 很容易忘记你有子图。谢谢你。真的帮了我大忙。
        【解决方案5】:

        您可以明确定义图例中的元素。

        为了完全控制哪些艺术家拥有图例条目,可以传递一个可迭代的图例艺术家,然后分别传递一个可迭代的图例标签。 Reference

        例子:

        arr1 = plt.arrow(0,0, 3,1, head_width=0.2, color='r', length_includes_head=True)
        arr2 = plt.arrow(0,0, 1,3, head_width=0.2, color='g', length_includes_head=True)
        arr3 = plt.arrow(0,0, 4,4, head_width=0.2, color='b', length_includes_head=True)
        
        plt.xlim(0,5)
        plt.ylim(0,5)
        
        plt.legend([arr1, arr2, arr3], ['u','v','u+v'])
        

        【讨论】:

        • 话虽如此,图例不应该与plt.arrow()s 中提供的标签一起使用吗?
        • @Yehuda 是的,plt.arrow() 的工作方式与 plt.plot() 类似,这是我们所要求的。奇怪的是,为什么这个解决方案受到如此多的支持,因为它没有解释为什么 plt.legend() 不理解 plt.arrow() 中标签的设置,尽管它应该。如果与 plt.plot() 中的方式相同,请参见 stackoverflow.com/questions/66862848/…stackoverflow.com/questions/64555759/…
        • @questionto42 1. 感谢您的链接和答案 2. 这是近一年来唯一可用的答案,因此得到了支持 ;)
        猜你喜欢
        • 2021-10-24
        • 1970-01-01
        • 2019-05-23
        • 2020-03-20
        • 1970-01-01
        • 2021-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多