加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 使用ArtistAnimation在matplotlib中制作动画

发布时间:2020-12-16 21:30:31 所属栏目:Python 来源:网络整理
导读:我一直在尝试使用有限元方法为2D热流问题创建一系列表面图.在每个时间步,我保存了一个图而不是整个矩阵,以便更有效率. 我在matplotlib.animation库中遇到了FuncAnimation问题,因此我决定每次渲染一个曲面图,将曲面图保存为.png文件,然后使用pyplot.imread读
我一直在尝试使用有限元方法为2D热流问题创建一系列表面图.在每个时间步,我保存了一个图而不是整个矩阵,以便更有效率.

我在matplotlib.animation库中遇到了FuncAnimation问题,因此我决定每次渲染一个曲面图,将曲面图保存为.png文件,然后使用pyplot.imread读取该图像.从那里,我想将每个图像存储到一个列表中,以便我可以使用ArtistAnimation(example).然而,它不是制作动画,而是当我将imgplot打印到屏幕时,我得到两个单独的空白图,然后是我的表面图.pngs.

此外,当我尝试保存动画时,我收到以下错误消息:

AttributeError: 'module' object has no attribute 'save'.

任何帮助从当前目录中读取一组.pngs,将它们保存在列表中,然后使用ArtistAnimation“动画”那些.pngs将非常感激.我不需要任何花哨的东西.

(注意 – 我必须使代码自动化,所以不幸的是我不能使用外部源来动画我的图像,如iMovie或ffmpeg.)

以下是我的代码:

from numpy import *
from pylab import *
import matplotlib.pyplot as plt 
import matplotlib.image as mgimg
from matplotlib import animation

## Read in graphs

p = 0
myimages = []

for k in range(1,len(params.t)):

  fname = "heatflow%03d.png" %p 
      # read in pictures
  img = mgimg.imread(fname)
  imgplot = plt.imshow(img)

  myimages.append([imgplot])

  p += 1


## Make animation

fig = plt.figure()
animation.ArtistAnimation(fig,myimages,interval=20,blit=True,repeat_delay=1000)

animation.save("animation.mp4",fps = 30)
plt.show()

解决方法

问题1:不显示图像

您需要将动画对象存储在变量中:

my_anim = animation.ArtistAnimation(fig,interval=100)

此要求特定于动画,并且与matplotlib中的其他绘图函数不一致,在matplotlib中,您通常可以无差别地使用my_plot = plt.plot()或plt.plot().

这个问题将在here进一步讨论.

问题2:保存不起作用

没有任何动画实例,也无法保存图形.这是因为save方法
属于ArtistAnimation类.你做的是从动画模块调用save,这就是引发错误的原因.

问题3:两个窗口

最后一个问题是你会弹出两个数字.原因是当你调用plt.imshow()时,它会在当前图形上显示一个图像,但由于还没有创建图形,pyplot会隐式为你创建一个图形.
当python稍后解释fig = plt.figure()语句时,它会创建一个新图形(另一个窗口)并将其标记为“图2”.
将此语句移动到代码的开头,可以解决该问题.

这是修改后的代码:

import matplotlib.pyplot as plt 
import matplotlib.image as mgimg
from matplotlib import animation

fig = plt.figure()

# initiate an empty  list of "plotted" images 
myimages = []

#loops through available png:s
for p in range(1,4):

    ## Read in picture
    fname = "heatflow%03d.png" %p 
    img = mgimg.imread(fname)
    imgplot = plt.imshow(img)

    # append AxesImage object to the list
    myimages.append([imgplot])

## create an instance of animation
my_anim = animation.ArtistAnimation(fig,interval=1000,repeat_delay=1000)

## NB: The 'save' method here belongs to the object you created above
#my_anim.save("animation.mp4")

## Showtime!
plt.show()

(要运行上面的代码,只需将3个图像添加到工作文件夹中,名称为“heatflow001.png”,通过“heatflow003.png”.)

使用FuncAnimation的替代方法

当你第一次尝试使用FuncAnimation时,你可能是对的,因为在列表中收集图像在内存方面是昂贵的.我通过比较上面的内存使用情况来测试下面的代码与上面的代码
系统监视器.似乎FuncAnimation方法更有效.我相信当你使用更多图像时,差异会变得更大.

这是第二个代码:

from matplotlib import pyplot as plt  
from matplotlib import animation  
import matplotlib.image as mgimg
import numpy as np

#set up the figure
fig = plt.figure()
ax = plt.gca()

#initialization of animation,plot array of zeros 
def init():
    imobj.set_data(np.zeros((100,100)))

    return  imobj,def animate(i):
    ## Read in picture
    fname = "heatflow%03d.png" % i 

    ## here I use [-1::-1],to invert the array
    # IOtherwise it plots up-side down
    img = mgimg.imread(fname)[-1::-1]
    imobj.set_data(img)

    return  imobj,## create an AxesImage object
imobj = ax.imshow( np.zeros((100,100)),origin='lower',alpha=1.0,zorder=1,aspect=1 )


anim = animation.FuncAnimation(fig,animate,init_func=init,repeat = True,frames=range(1,4),interval=200,repeat_delay=1000)

plt.show()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读