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

ความแตกต่างระหว่าง Matplotlib.pyplot และ Matplotlib.figure

matplotlib.pyplot

matplotlib.pyplot เป็นคอลเลกชันของฟังก์ชันที่ทำให้ Matplotlib ทำงานคล้าย MATLAB แต่ละฟังก์ชัน pyplot จะทำการเปลี่ยนแปลงบน figure: สร้าง figure, สร้างพื้นที่พล็อต, พล็อตเส้น, ตกแต่ง labels, เป็นต้น

ใน matplotlib.pyplot สถานะต่างๆ จะถูกเก็บรักษาม้ามห้ามข้ามการเรียกฟังก์ชัน จึงติดตามได้ว่าฟีเจอร์ไหนเป็นปัจจุบัน และฟังก์ชันพล็อตจะถูกนำไปที่ axes ปัจจุบัน

matplotlib.figure

Figure object เก็บรายการ child Axes ทั้งหมด ศิลปินพิเศษ (titles, figure legends, ฯลฯ) และ canvas Figure สามารถมี Axes ได้หลายตัว แต่โดยทั่วไปจะมีอย่างน้อยหนึ่งตัว

ตัวอย่าง

from matplotlib import pyplot as plt

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

fig = plt.figure("I am figure window")
plt.show()

คำอธิบาย

  • pyplot: interface แบบ stateful (MATLAB-style)
  • figure: container object แบบ OO (object-oriented)
  • แนะนำให้ใช้ OO interface (fig, ax = plt.subplots()) สำหรับโค้ดซับซ้อน