pl.legend和plt.legend使用标签label报错

import matplotlib.pyplot as plt
import numpy as np
import pylab as pl

x1=[1,2,3,4,5]
y1=[1,4,9,16,25]

x2=[1,2,4,6,8]
y2=[2,4,8,12,16]

plot1=pl.plot(x1, y1, "r")
plot2=pl.plot(x2,y2,"go")

pl.title("Plot of y vs. x")

pl.xlabel("x axis")
pl.ylabel("y axis")

pl.xlim(0.0,  9.0)
pl.ylim(0.0,  30.0)

pl.legend([plot1, plot2],  ("red line", "green circles"), "best", numpoints=1)
##pl.legend([plot1, plot2])

pl.show()

结果能显示,但是无法显示标签label。

figure_1.png

报错信息:

Warning (from warnings module):
  File "C:\Python27\lib\site-packages\matplotlib\legend.py", line 613
    (str(orig_handle),))
UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x05B081F0>]
Use proxy artist instead.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist


Warning (from warnings module):
  File "C:\Python27\lib\site-packages\matplotlib\legend.py", line 613
    (str(orig_handle),))
UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x05B083D0>]
Use proxy artist instead.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

=======================================================
修正代码:

import matplotlib.pyplot as plt
import numpy as np
import pylab as pl

x1=[1,2,3,4,5]
y1=[1,4,9,16,25]

x2=[1,2,4,6,8]
y2=[2,4,8,12,16]

plot1, =pl.plot(x1, y1, "r")                  #   这个 ,一定要有,不然legend会报错
plot2, =pl.plot(x2,y2,"go")

pl.title("Plot of y vs. x")

pl.xlabel("x axis")
pl.ylabel("y axis")

pl.xlim(0.0,  9.0)
pl.ylim(0.0,  30.0)

pl.legend([plot1, plot2],  ("red line", "green circles"), "best", numpoints=1)
##pl.legend([plot1, plot2])

pl.show()

图示:

figure_2.png

终于解决了TnT

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 因为要结局swift3.0中引用snapKit的问题,看到一篇介绍Xcode8,swift3变化的文章,觉得很详细...
    uniapp阅读 4,556评论 0 12
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,895评论 25 709
  • HTML5 标签comment 注释标签用于在源文档中插入注释。注释内容不会被浏览器显示。为代码编写注释的好处是...
    才気莮孒阅读 4,123评论 1 25
  • 许久不爱,身体开始老去 你应该好好爱抚我 如饮茶前仔细观察一块陈皮 最后丢进水里使之变得轻浮 而我在一张异常空旷的...
    鸢尾blue阅读 268评论 1 6
  • 从Xcode8.0出来之后.一夜之间貌似所有的插件都消失了.对于很多开发者来说是一件很恐怖的事情.比如说我,没有了...
    请叫我Hank阅读 3,273评论 16 16