ขั้นตอนการทำงาน
- กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
- พล็อตเส้นด้วยสองรายการ พร้อม label, linewidth และ linestyle ต่างกัน
- วาง legend แรกที่ upper-right
- เพิ่ม artist คือ legend แรกเข้าไปใน axes ปัจจุบัน
- วาง legend ที่สองบน axes ปัจจุบันที่ lower-right
- ใช้เมธอด show() เพื่อแสดงรูปภาพ
ตัวอย่างโค้ด
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="Line 1", linestyle='--') line2, = plt.plot([3, 2, 1], label="Line 2", linewidth=4) first_legend = plt.legend(handles=[line1], loc='upper right') plt.gca().add_artist(first_legend) plt.legend(handles=[line2], loc='lower right') plt.show()
ผลลัพธ์
