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

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

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() 中的注释文本从不更新
  • 画布被清除和覆盖,导致注释消失

解决的五个步骤:

  1. 移除 fig.clear() 以保留注释
  2. 保存初始注释的句柄
  3. updatefig() 中更新注释的文本
  4. updatefig()return 中包括注释
  5. 设置 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)

使用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
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>

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:

确定