เพื่อ refresh (รีเฟรช/อัปเดต) ข้อความใน Matplotlib
- ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
- สร้าง figure และ subplots
- เพิ่มข้อความบนแกน
- เขียนฟังก์ชันอัปเดตข้อความตามปุ่ม "z" และ "c"
- ผูกฟังก์ชันกับ event key_press_event
- วาด canvas ที่มี figure
- ทำ animation พร้อมข้อความ
- แสดงรูปด้วย show()
ตัวอย่าง
from matplotlib import pyplot as plt, animation
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
text = ax.text(.5, .5, 'First Text')
def action(event):
if event.key == "z":
text.set_text("zoom")
elif event.key == "c":
text.set_text("cool")
fig.canvas.mpl_connect('key_press_event', action)
fig.canvas.draw()
animation = animation.ArtistAnimation(fig, [(text,)])
plt.show()ผลลัพธ์
