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

python – 使用ArtistAnimation在matplotlib中制作动画,第1张

概述我一直在尝试使用有限元方法为2D热流问题创建一系列表面图.在每个时间步,我保存了一个图而不是整个矩阵,以便更有效率. 我在matplotlib.animation库中遇到了FuncAnimation问题,因此我决定每次渲染一个曲面图,将曲面图保存为.png文件,然后使用pyplot.imread读取该图像.从那里,我想将每个图像存储到一个列表中,以便我可以使用ArtistAnimation(exa 我一直在尝试使用有限元方法为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 mgimgfrom matplotlib import animation## Read in graphsp = 0myimages = []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 animationfig = 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:两个窗口

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

这是修改后的代码:

import matplotlib.pyplot as plt import matplotlib.image as mgimgfrom matplotlib import animationfig = plt.figure()# initiate an empty  List of "plotted" images myimages = []#loops through available png:sfor 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 animationmy_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 mgimgimport numpy as np#set up the figurefig = 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 objectimobj = 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()
总结

以上是内存溢出为你收集整理的python – 使用ArtistAnimation在matplotlib中制作动画全部内容,希望文章能够帮你解决python – 使用ArtistAnimation在matplotlib中制作动画所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1204936.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存