在下面的代碼中,圖例應該是串列中指定的名稱,但正如您在圖中看到的那樣,只顯示了第一個字母。
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()


我該如何解決?
uj5u.com熱心網友回復:
你legend沒有正確使用,你需要使用label引數。
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()

uj5u.com熱心網友回復:
plt.legend()接受一個可迭代的標簽,因此如果您只需要其他所有內容不變的標簽,您可以替換
plt.legend(b)
和
plt.legend([b])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/495132.html
標籤:Python matplotlib 传奇
