使用Matplotlib中的FuncAnimation来实现标签动画。

huangapple go评论145阅读模式
英文:

Animate labels using FuncAnimation in Matplotlib

问题

  1. # 不要翻译代码部分
  2. # does not work
  3. 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?

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. fig, ax = plt.subplots()
  5. fig.clear()
  6. steps = 10
  7. data = np.random.rand(20,20,10)
  8. imagelist = [data[:,:,i] for i in range(steps) ]
  9. im = plt.imshow(imagelist[0], cmap='Greys', origin='lower', animated=True)
  10. plt.colorbar(shrink=1, aspect=30, label='Counts')
  11. # does not work
  12. ax.annotate("Frame: %d " % steps,(0.09,0.92),xycoords ='figure fraction')
  13. def updatefig(j):
  14. im.set_array(imagelist[j])
  15. return [im]
  16. ani = animation.FuncAnimation(fig, updatefig, frames=range(steps), interval=200, blit=True)
  17. plt.show()

答案1

得分: 0

以下是翻译好的部分:

两个总体问题:

  • updatefig() 中的注释文本从不更新
  • 画布被清除和覆盖,导致注释消失

解决的五个步骤:

  1. 移除 fig.clear() 以保留注释
  2. 保存初始注释的句柄
  3. updatefig() 中更新注释的文本
  4. updatefig()return 中包括注释
  5. 设置 blit=False 以保留注释
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. fig, ax = plt.subplots()
  5. #1 不要调用 fig.clear()
  6. steps = 10
  7. data = np.random.rand(20, 20, steps)
  8. im = plt.imshow(data[:, :, 0], cmap='Greys', origin='lower', animated=True)
  9. plt.colorbar(shrink=1, aspect=30, label='Counts')
  10. #2 注释第0帧并保存句柄
  11. annot = ax.annotate('Frame: 0', (0.09, 0.92), xycoords='figure fraction')
  12. def updatefig(j):
  13. im.set_array(data[:, :, j])
  14. #3 更新注释文本
  15. annot.set_text(f'Frame: {j}')
  16. #4 在返回时包括注释
  17. return im, annot
  18. #5 设置 blit=False
  19. anim = animation.FuncAnimation(fig, updatefig, frames=steps, blit=False)

使用Matplotlib中的FuncAnimation来实现标签动画。

英文:

Two problems overall:

  • The annotation text never gets updated in updatefig()
  • The canvas gets cleared+blitted, which wipes out annotations

Five steps to resolve:

  1. Remove fig.clear() to preserve annotations
  2. Save the initial annotation's handle
  3. Update the annotation's text in updatefig()
  4. Include the annotation in the return of updatefig()
  5. Set blit=False to preserve annotations
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. fig, ax = plt.subplots()
  5. #1 do NOT call fig.clear()
  6. steps = 10
  7. data = np.random.rand(20, 20, steps)
  8. im = plt.imshow(data[:, :, 0], cmap='Greys', origin='lower', animated=True)
  9. plt.colorbar(shrink=1, aspect=30, label='Counts')
  10. #2 annotate frame 0 and save handle
  11. annot = ax.annotate('Frame: 0', (0.09, 0.92), xycoords='figure fraction')
  12. def updatefig(j):
  13. im.set_array(data[:, :, j])
  14. #3 update annotation text
  15. annot.set_text(f'Frame: {j}')
  16. #4 include annotation when returning
  17. return im, annot
  18. #5 set blit=False
  19. 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>

huangapple
  • 本文由 发表于 2023年2月18日 18:12:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定