【问题标题】:How to animate plt.imshow with text?如何使用文本为 plt.imshow 设置动画?
【发布时间】:2023-01-05 13:14:59
【问题描述】:

我正在尝试为模拟制作动画。我想包括模拟时间。我写了以下代码:

import matplotlib.animation as animation

fig, ax = plt.subplots()

ims = []
for i in range(40):
    im=plt.imshow(np.log10(D[0,i,:,:]),cmap=plt.get_cmap("Spectral"),extent=[0,28,0,14],animated=True)
    plt.text(10,2,"t="+str(t[i])+"Myr",c='w',fontsize='large')
    ims.append([im])

ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
                                repeat_delay=1000)

ani.save("rhdjet1.mp4")
plt.show()

但是一开始所有的文本都被立即转储了。This is a still from the animation. The gibberish in white is the text getting overlayed. 如何纠正这个?

【问题讨论】:

    标签: python-3.9 matplotlib-animation


    【解决方案1】:

    这可以使用 python 中的 celluloid 包来完成:

    from celluloid import Camera
    
    #Creating matplotlib figure and camera object
    fig = plt.figure()
    
    camera = Camera(fig)
     
    #Looping the data and capturing frame at each iteration
    for i in range(0,90):
          plt.imshow(K[i])
          plt.text(str(t[i]))
          camera.snap()
     
    #Creating the animation from captured frames
    animation = camera.animate(interval = 200, repeat = True,
                               repeat_delay = 500)
    animation.save("restart.mp4")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      相关资源
      最近更新 更多