use*_*736 5 python animation matplotlib
我一直在尝试使用有限元方法为2D热流问题创建一系列表面图.在每个时间步,我保存了一个图而不是整个矩阵,以便更有效率.
我FuncAnimation在matplotlib.animation库中遇到了麻烦,所以我决定每次都渲染一个曲面图,将曲面图保存为.png文件,然后使用它来读取该图像pyplot.imread.从那里,我想将每个图像存储到一个列表中,以便我可以使用ArtistAnimation(示例).然而,它不是制作动画,而是当我打印imgplot到屏幕时,我得到两个单独的空白图,然后我的表面图.pngs .
此外,当我尝试保存动画时,我收到以下错误消息:
AttributeError: 'module' object has no attribute 'save'.
Run Code Online (Sandbox Code Playgroud)
任何帮助从当前目录中读取一组.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()
Run Code Online (Sandbox Code Playgroud)
问题 1:图像不显示
您需要将动画对象存储在变量中:
my_anim = animation.ArtistAnimation(fig, myimages, interval=100)
Run Code Online (Sandbox Code Playgroud)
此要求是针对 中的animation其他绘图功能所特有的,并且与 中的其他绘图功能不一致matplotlib,您通常可以使用my_plot=plt.plot()或plt.plot()无所谓。
这个问题在这里进一步讨论。
问题 2:保存不起作用
没有任何animation实例,也无法保存图形。这是因为该save方法属于ArtistAnimation该类。您所做的是save从animation模块调用,这就是引发错误的原因。
问题 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, myimages, interval=1000, blit=True, repeat_delay=1000)
## NB: The 'save' method here belongs to the object you created above
#my_anim.save("animation.mp4")
## Showtime!
plt.show()
Run Code Online (Sandbox Code Playgroud)
(要运行上面的代码,只需将 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, blit=True, repeat_delay=1000)
plt.show()
Run Code Online (Sandbox Code Playgroud)