I am trying to make an animation using cartopy axes and matplotlib. Any way I have tried either plots directly over the data not clearing the previous image, or it generates multiple panels resulting in an animation that look this:
Here is the code that generated (that somewhat cool, but not what I was going for) animation:
import pylab as PP
fig = PP.figure(figsize=(10, 6))
#ax = fig.add_subplot(1,1,1,projection=ccrs.Robinson(central_longitude=270))
ax = PP.axes(projection=ccrs.Robinson(central_longitude=270))
frames = eofs.shape[0] # Number of frames
eof_num=0
def init():
return animate(0)
def animate(frame):
cdata,clons = add_cyclic_point(eofs[frame,eof_num,...],_z500.lon)
levels = np.linspace(-60,60,21)
cplt = []
cplt = ax.contourf(clons, _z500.lat, cdata, transform=ccrs.PlateCarree(), levels=levels, cmap='RdBu_r')
sigLats, sigLons = nbh.sig_pts_from_pvalues(eof_reg_pvals[frame,eof_num,...],_z500.lat,_z500.lon)
PP.plot(sigLons[::4], sigLats[::4], 'k.', markersize=1, transform=ccrs.PlateCarree())
ax.coastlines()
ax.set_global()
ax.set_title('EOF{} | SEOF#{} | {}'.format(eof_num+1,frame+1,_z500.dates[:-1][::5][frame]))
cb = PP.colorbar(cplt,fraction=0.02)
cb.set_label(label='z500 [m]', fontsize=12)
return(cplt)
ani = animation.FuncAnimation(fig, animate, frames, interval=0.01, blit=False,
init_func=init, repeat=True)
ani.save('test.mp4', writer=animation.FFMpegWriter(fps=4))
PP.close(fig)
Cant figure out what I am doing wrong here.
Thanks!
Have a look to this post.
You should clear the ax
at the start of the animate
function with ax.cla()
and move the colorbar definition outside that function.