pyplot combine multiple line labels in legend

前端 未结 6 2126
鱼传尺愫
鱼传尺愫 2020-12-29 07:00

I have data that results in multiple lines being plotted, I want to give these lines a single label in my legend. I think this can be better demonstrated using the example b

6条回答
  •  天涯浪人
    2020-12-29 07:41

    A low tech solution is to make two plot calls. One that plots your data and a second one that plots nothing but carries the handle:

    a = np.array([[ 3.57,  1.76,  7.42,  6.52],
                  [ 1.57,  1.2 ,  3.02,  6.88],
                  [ 2.23,  4.86,  5.12,  2.81],
                  [ 4.48,  1.38,  2.14,  0.86],
                  [ 6.68,  1.72,  8.56,  3.23]])
    
    plt.plot(a[:,::2].T, a[:, 1::2].T, 'r')
    plt.plot([],[], 'r', label='data_a')
    
    plt.legend(loc='best')
    

    Here's the result:

    result

提交回复
热议问题