typeerror in basic example for legend handles in matplotlib

后端 未结 3 1836
独厮守ぢ
独厮守ぢ 2021-01-19 20:23

I have difficulties to understand the legend handling. The more, the basic example from the official matplotlib legend guide

import matplotlib.pyplot as plt         


        
相关标签:
3条回答
  • 2021-01-19 20:28

    Just remove handles keyword

    Use it like that:

    import matplotlib.pyplot as plt
    line_up, = plt.plot([1,2,3], label='Line 2')
    line_down, = plt.plot([3,2,1], label='Line 1')
    plt.legend([line_up, line_down])
    
    0 讨论(0)
  • 2021-01-19 20:39

    I had the same error some while back, but the fixes suggested above didn't work for me. I updated my version of matplotlib as well, but this didn't help.

    What did work was removing the handles argument and which plots to label altogether in the legend() method; like this:

        plot1 = plt.plot([1,2,3], 'b', label = 'first plot')
        plot2 = plt.plot([3,2,1], 'r', label = 'second plot')
        plt.legend()
        plt.show()
    

    Which rendered nicely to this:

    0 讨论(0)
  • 2021-01-19 20:40

    I was having the same issue as Jan, running Matplotlib 1.3.1 on Ubuntu 14.04. I tried the answer posted by Kobi K. His code did not raise any errors. However, the legend did not render correctly: I upgraded to Matplotlib 1.5.1, and can now render the legend correctly using the code posted by Jan, which includes the 'handles' keyword (i.e. the code that appears in the Matplotlib legend guide):

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