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

ล้าง figure ทั้งหมดใน matplotlib

ในการล้าง figure ทั้งหมดใน matplotlib ใช้เมธอด close('all')

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots

  • สร้าง figure ชื่อ "First Figure"

  • สร้าง figure อีกอันชื่อ "Second Figure"

  • ปิด figure ทั้งหมดด้วย close('all')

  • แสดงรูปด้วยเมธอด show()

ตัวอย่าง

from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.figure("First Figure")
plt.figure("Second Figure")
# plt.close('all')
plt.show()

ผลลัพธ์

สังเกตว่าบรรทัด plt.close('all') ถูก comment ไว้ ดังนั้นจะแสดงสอง figure

plt.close('all')

ล้าง figure ทั้งหมดใน matplotlib

ล้าง figure ทั้งหมดใน matplotlib

Uncomment บรรทัด plt.close('all') และรันอีกครั้ง จะล้าง figure ทั้งหมด