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

วิธีปิดหน้าต่าง pyplot ทั้งหมดใน Matplotlib


คำสั่งปิด figure windows ต่างๆ

  • plt.figure().close(): ปิดหน้าต่าง figure
  • close() ปิด figure ปัจจุบัน
  • close(h) โดย h เป็น Figure instance ปิด figure นั้น
  • close(num) ปิด figure ที่มี number=num
  • close(name) โดย name เป็น string ปิด figure ที่มี label นั้น
  • close('all') ปิดหน้าต่าง figure ทั้งหมด

ตัวอย่างโค้ด

from matplotlib import pyplot as plt

fig = plt.figure()
ax = fig.add_subplot()
plt.show()
plt.close()

ผลลัพธ์

วิธีปิดหน้าต่าง pyplot ทั้งหมดใน Matplotlib

หากสลับคำสั่ง "plt.show()" และ "plt.close()" จะไม่เห็นกราฟ เพราะถูกปิดก่อนแสดง