美文网首页
matplotlib库 UserWarning: Legend

matplotlib库 UserWarning: Legend

作者: 我的章鱼小丸子呢 | 来源:发表于2021-06-18 10:08 被阅读0次

    场景

    在使用 matplotlib.pyplot 画图添加图例:

    fig, ax1 = plt.subplots()line1 = ax1.plot(x, y, color='firebrick')  # draw a lineax2.legend([line1], ['First']) 
    

    错误信息

    显示以下提示:


    错误信息

    原因与解决方案

    原因在于,plot 返回 的 list 对象(list of Line2D)需要解构,因此需要在line1和等号之间加一个逗号:

    fig, ax1 = plt.subplots()line1, = ax1.plot(x, y, color='firebrick')  # draw a lineax2.legend([line1], ['First'])
    

    相关文章

      网友评论

          本文标题:matplotlib库 UserWarning: Legend

          本文链接:https://www.haomeiwen.com/subject/mqrkyltx.html