英文:
Animate labels using FuncAnimation in Matplotlib
问题
# 不要翻译代码部分
# does not work
ax.annotate("Frame: %d " % steps, (0.09, 0.92), xycoords='figure fraction')
英文:
I am not able to make (animated) labels using FuncAnimation
from matplotlib. Please find below a minimal code that I made. ax.annotate
has no effect at all - the animation itself works though. What can I change to get animated labels/titles, which are different for each frame?
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
fig.clear()
steps = 10
data = np.random.rand(20,20,10)
imagelist = [data[:,:,i] for i in range(steps) ]
im = plt.imshow(imagelist[0], cmap='Greys', origin='lower', animated=True)
plt.colorbar(shrink=1, aspect=30, label='Counts')
# does not work
ax.annotate("Frame: %d " % steps,(0.09,0.92),xycoords ='figure fraction')
def updatefig(j):
im.set_array(imagelist[j])
return [im]
ani = animation.FuncAnimation(fig, updatefig, frames=range(steps), interval=200, blit=True)
plt.show()
答案1
得分: 0
以下是翻译好的部分:
两个总体问题:
updatefig()
中的注释文本从不更新- 画布被清除和覆盖,导致注释消失
解决的五个步骤:
- 移除
fig.clear()
以保留注释 - 保存初始注释的句柄
- 在
updatefig()
中更新注释的文本 - 在
updatefig()
的return
中包括注释 - 设置
blit=False
以保留注释
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
#1 不要调用 fig.clear()
steps = 10
data = np.random.rand(20, 20, steps)
im = plt.imshow(data[:, :, 0], cmap='Greys', origin='lower', animated=True)
plt.colorbar(shrink=1, aspect=30, label='Counts')
#2 注释第0帧并保存句柄
annot = ax.annotate('Frame: 0', (0.09, 0.92), xycoords='figure fraction')
def updatefig(j):
im.set_array(data[:, :, j])
#3 更新注释文本
annot.set_text(f'Frame: {j}')
#4 在返回时包括注释
return im, annot
#5 设置 blit=False
anim = animation.FuncAnimation(fig, updatefig, frames=steps, blit=False)
英文:
Two problems overall:
- The annotation text never gets updated in
updatefig()
- The canvas gets cleared+blitted, which wipes out annotations
Five steps to resolve:
- Remove
fig.clear()
to preserve annotations - Save the initial annotation's handle
- Update the annotation's text in
updatefig()
- Include the annotation in the
return
ofupdatefig()
- Set
blit=False
to preserve annotations
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
#1 do NOT call fig.clear()
steps = 10
data = np.random.rand(20, 20, steps)
im = plt.imshow(data[:, :, 0], cmap='Greys', origin='lower', animated=True)
plt.colorbar(shrink=1, aspect=30, label='Counts')
#2 annotate frame 0 and save handle
annot = ax.annotate('Frame: 0', (0.09, 0.92), xycoords='figure fraction')
def updatefig(j):
im.set_array(data[:, :, j])
#3 update annotation text
annot.set_text(f'Frame: {j}')
#4 include annotation when returning
return im, annot
#5 set blit=False
anim = animation.FuncAnimation(fig, updatefig, frames=steps, blit=False)
<a href="https://i.stack.imgur.com/EtBJi.gif"><img src="https://i.stack.imgur.com/EtBJi.gif" width="480"></a>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论