Matplotlib Legends not working

后端 未结 4 1305
无人及你
无人及你 2020-12-07 10:51

Ever since upgrading matplotlib I get the following error whenever trying to create a legend:

/usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarn         


        
相关标签:
4条回答
  • 2020-12-07 11:11

    use label while plotting graph then only u can use legend. x axis name and y axis name is different than legend name.

    0 讨论(0)
  • 2020-12-07 11:14

    Use handles AKA Proxy artists

    import matplotlib.lines as mlines
    import matplotlib.pyplot as plt
    # defining legend style and data
    blue_line = mlines.Line2D([], [], color='blue', label='My Label')
    reds_line = mlines.Line2D([], [], color='red', label='My Othes')
    
    plt.legend(handles=[blue_line, reds_line])
    
    plt.show()
    
    0 讨论(0)
  • 2020-12-07 11:25

    You should add commas:

    plot1, = plt.plot(a,b)
    plot2, = plt.plot(a,c)
    

    The reason you need the commas is because plt.plot() returns a tuple of line objects, no matter how many are actually created from the command. Without the comma, "plot1" and "plot2" are tuples instead of line objects, making the later call to plt.legend() fail.

    The comma implicitly unpacks the results so that instead of a tuple, "plot1" and "plot2" automatically become the first objects within the tuple, i.e. the line objects you actually want.

    http://matplotlib.sourceforge.net/users/legend_guide.html#adjusting-the-order-of-legend-items

    line, = plot(x,sin(x)) what does comma stand for?

    0 讨论(0)
  • 2020-12-07 11:36

    Use the "label" keyword, like so:

    pyplot.plot(x, y, label='x vs. y')
    

    and then add the legend like so:

    pyplot.legend()
    

    The legend will retain line properties like thickness, colours, etc.

    0 讨论(0)
提交回复
热议问题