【问题标题】:Matplotlib: adding legend for each column of numpy arrayMatplotlib:为numpy数组的每一列添加图例
【发布时间】:2017-11-08 12:38:58
【问题描述】:

嗨,我有一个 numpy 数组

a = np.random.uniform(0,1, size = (10,3))

我想用自己的标签绘制每一列

plt.plot(a, label = ['label1', 'label2', 'label3'])
plt.legend()

我该怎么做?以上是我的试探性,但没有奏效。

【问题讨论】:

    标签: python python-3.x matplotlib legend


    【解决方案1】:

    稍微短一点的方法(因为图例中已经存在句柄):

    import numpy as np
    import matplotlib.pyplot as plt
    
    a = np.random.uniform(0,1, size = (10,3))
    
    plt.plot(a)
    plt.legend(['label1', 'label2', 'label3'])
    
    plt.show()
    

    【讨论】:

      【解决方案2】:

      我想我找到了here

      l1,l2, l3 = plt.plot(a)
      plt.legend((l1,l2, l3), ('label1', 'label2', 'label3'))
      

      【讨论】:

        猜你喜欢
        • 2020-12-06
        • 1970-01-01
        • 2016-05-23
        • 1970-01-01
        • 2014-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多