Để gán nhãn cho đường vẽ và hiển thị trong legend, truyền tham số label="..." vào plt.plot() rồi gọi plt.legend().
Các bước
- Thiết lập figure size và autolayout.
- Vẽ đường 1: plt.plot([1,2,3], label="line1").
- Vẽ đường 2: plt.plot([3,2,1], label="line2").
- Hiển thị legend: plt.legend(loc='upper center').
- Hiển thị bằng show().
Ví dụ
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
line1, = plt.plot([1, 2, 3], label="line1")
line2, = plt.plot([3, 2, 1], label="line2")
leg = plt.legend(loc='upper center')
plt.show()
Kết quả
