Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีแก้ไข Legend ของ Matplotlib หลังจากสร้างแล้ว


เพื่อแก้ไข Legend ของ Matplotlib หลังจากสร้างแล้ว มีหลายวิธีในการแก้ไข Legend ที่สร้างขึ้น

  • กำหนดขนาดรูปและปรับระยะห่างระหว่างและรอบซับพล็อต
  • พล็อตเส้นด้วย plot() พร้อมสองลิสต์และ label
  • ใช้ legend() วาง Legend บนกราฟ
  • แก้ไข Legend ด้วย set_title()
  • ใช้ show() เพื่อแสดงรูป

ตัวอย่าง

from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

plt.plot([1, 3, 4, 5, 2, 1], [3, 4, 1, 3, 0, 1], label="line plot", color='red', lw=0.5)
leg = plt.legend(loc="upper right")
leg.set_title("Title")
plt.show()

ผลลัพธ์

วิธีแก้ไข Legend ของ Matplotlib หลังจากสร้างแล้ว