【问题标题】:plt.legend() onyl shows the first letter of the legend stringplt.legend() 只显示图例字符串的第一个字母
【发布时间】:2022-06-19 22:00:52
【问题描述】:

在下面的代码中,图例应该是列表中指定的名称,但是如图所示,只显示了第一个字母。

bench = ['AA', 'BB']
offset = 0
for b in bench:
    L1 = [12+offset, 5+offset, 3+offset]
    L2 = [20+offset, 22+offset, 25+offset]
    offset += 5
    
    plt.plot(L1, L2)
    plt.legend(b)
    plt.savefig('test4.png')
    plt.show()

我该如何解决这个问题?

【问题讨论】:

    标签: python matplotlib legend


    【解决方案1】:
    import matplotlib.pyplot as plt
    
    bench = ['AA', 'BB']
    offset = 0
    for b in bench:
        L1 = [12+offset, 5+offset, 3+offset]
        L2 = [20+offset, 22+offset, 25+offset]
        offset += 5
        
        plt.plot(L1, L2, label=b)
        plt.legend()
        plt.savefig('test4.png')
        plt.show()
    

    【讨论】:

      猜你喜欢
      • 2014-03-01
      • 2012-07-15
      • 2014-05-30
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多