How do I assign multiple labels at once in matplotlib?

前端 未结 9 1497
半阙折子戏
半阙折子戏 2020-12-02 16:41

I have the following dataset:

x = [0, 1, 2, 3, 4]
y = [ [0, 1, 2, 3, 4],
      [5, 6, 7, 8, 9],
      [9, 8, 7, 6, 5] ]

Now I plot it with:

9条回答
  •  伪装坚强ぢ
    2020-12-02 17:02

    You can give the labels while plotting the curves

    import pylab as plt
    
    x = [0, 1, 2, 3, 4]
    y = [ [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [9, 8, 7, 6, 5] ]
    labels=['foo', 'bar', 'baz']
    colors=['r','g','b']
    
    # loop over data, labels and colors
    for i in range(len(y)):
        plt.plot(x,y[i],'o-',color=colors[i],label=labels[i])
    
    plt.legend()
    plt.show()
    

    enter image description here

提交回复
热议问题