ในการรีเฟรชข้อความใน Matplotlib เราสามารถทำตามขั้นตอนต่อไปนี้ -
- กำหนดขนาดรูปและปรับช่องว่างภายในระหว่างและรอบๆ แผนผังย่อย
- สร้างร่างและชุดแผนย่อย
- เพิ่มข้อความลงในแกน
- เขียนวิธีการที่กำหนดเองเพื่ออัปเดตข้อความตามคีย์ "z" และ "c"
- เชื่อมโยงการทำงานของฟังก์ชันด้วย key_press_event .
- วาดภาพแคนวาสที่มีฟิกเกอร์
- ทำให้ร่างเคลื่อนไหวด้วยข้อความ
- หากต้องการแสดงรูป ให้ใช้ 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() ผลลัพธ์
