应用plt.legend报错UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x09B441D0>]

应用plt.legend报错UserWarning: Legend does not support [

应用plt.legend报错UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x09B441D0>] instances.

在用plt.plot画图时,如果一张图上需要画多条曲线,此时需要给每条曲线添加标签,需要用到plt.legend。主要代码如下:
l1 = plt.plot(Ef[0:9], “x-”)
l2 =plt.plot(Ef[9:18], “±”)
plt.legend([l1,l2],[‘first’,“second”], loc=‘upper right’)

运行程序,此时报错:
报错信息
需要将代码修改为:
l1, = plt.plot(Ef[0:9], “x-”)
l2, =plt.plot(Ef[9:18], “±”)
plt.legend([l1,l2],[‘first’,“second”], loc=‘upper right’)

此时图片可以正常显示:
在这里插入图片描述
你需要逗号的原因是因为plt.plot()返回一个行对象的元组,无论从命令实际创建了多少个。如果没有逗号,“plot1”和“plot2”是元组而不是行对象,这使得后来对plt.legend()的调用失败。

逗号隐式地解压缩结果,以便代替元组,“plot1”和“plot2”自动成为元组中的第一个对象,即您实际想要的线对象。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值