2016-07-16 62 views
1

我试图保存一个GIF与2d使用pcolormesh(使用表面或线框也可以)一些波的演变。高效地动画pcolormesh

这一直是我的形式给出了到目前为止: 设置quadmesh极坐标绘制:

from matplotlib import pyplot 
from matplotlib.animation import FuncAnimation as FuncAnimation 
pi=np.pi 


rmax=6. 
r=2*np.linspace(0,np.sqrt(rmax*.5),100)**2 
phi=np.linspace(0,2*pi,80) 

R, P = np.meshgrid(r, phi) 
X, Y = R*np.cos(P), R*np.sin(P) 

设定的数字和功能动画: 数是我的帧的数量。 Z是具有我想绘制的值的count * 2D数组。 (它有一个像系列某些傅立叶的总和)

fig, ax = pyplot.subplots() 
def anim_I(count,r,phi): 
    anim=np.zeros((count,len(phi), len(r))) 
    for i in range(count): 
     anim[i,:,:]=coef_transf(final_coefs[i,:,:,:,0],r,phi)**2 
    return anim 

Z=anim_I(count,r,phi) 
def animate(i): 
    pyplot.title('Time: %s'%time[i]) 
    #This is where new data is inserted into the plot. 
    plot=ax.pcolormesh(X, Y,Z[i,:,:],cmap=pyplot.get_cmap('viridis'),vmin=0., vmax=15.) 
    return plot, 

ax.pcolormesh(X, Y,Z[0,:,:],cmap=pyplot.get_cmap('viridis'),vmin=0., vmax=15.) 
pyplot.colorbar() 

anim = FuncAnimation(fig, animate, frames = range(0,count,7), blit = False) 

我并不真的需要看直播,所以我只是保存GIF。

anim.save('%d_%d_%d-%d.%d.%d-2dgif.gif' %(localtime()[0:6]), writer='imagemagick')  
pyplot.close() 

虽然这可行,但可能需要一个小时才能制作出甚至上百帧的gif。

我不知道什么是正确的方法来做到这一点,因此它可以使用。

我已经看到了这方面的其他文章,但我无法得到代码工作,或者它会一样无用。

回答

0

你可以试着写的

def animate(i): 
pyplot.title('Time: %s'%time[i]) 
#This is where new data is inserted into the plot. 
plot=plot.set_array(Z[i,:,:].ravel()) 
return plot, 

代替

def animate(i): 
pyplot.title('Time: %s'%time[i]) 
#This is where new data is inserted into the plot. 
plot=ax.pcolormesh(X, Y,Z[i,:,:],cmap=pyplot.get_cmap('viridis'),vmin=0., vmax=15.) 
return plot, 

这不会创建一个新的对象每次调用的动画funtion时间。相反,它会更改已创建的对象的图像。 但是,set_array方法似乎需要一个扁平数组,因此.ravel()。

如果将pcolormap函数的着色选项设置为shading ='gouraud',则只会生成正确的图像。 我不知道为什么,不幸的是,它似乎与排序数组有关。

我希望这会有所帮助。