问题描述
我一直试图在地图上创建浓度的contourf表示的动画,我一直使用的主要代码是:
tiler = Stamen('terrain-background')
fig,ax = plt.subplots(figsize=[10,10],subplot_kw=dict(projection=ccrs.AlbersEqualArea()))
ax.set_extent([-6.5,-4.5,35.5,37])
ax.add_image(tiler,11)
ax.gridlines(draw_labels=True,dms=True,x_inline=False,y_inline=False)
cf = ax.contourf(LON,LAT,Z[:,:,0]*np.nan,levels=[1e-3,1e-2,1e-1,10,100,1000],cmap=plt.get_cmap('gist_heat_r'),alpha = 0.65,antialiased=True,norm = LogNorm(),transform=ccrs.PlateCarree()
)
cbar = fig.colorbar(cf)
cbar.set_label('mg/m^3')
def update(i):
ax.contourf(LON,i],transform=ccrs.PlateCarree()
)
plt.title('{} average concentration. date: {}'.format(avg_label[i],date_label[i]))
ani = animation.FuncAnimation(fig,update,frames=Z.shape[2],interval=1500)
plt.show()
一切都可以,但contourf表示法不会在每个步骤中刷新信息。在每个步骤中,仍然会显示较旧帧的先前表示形式。因此,最后,最后一帧的结果等于四个表示帧的重叠。
如何在不丢失背景图的情况下更改此行为?我只想在每个帧中显示Z [:,:,i]。
提前谢谢!
解决方法
一系列补丁存储在集合中。每次称为更新时,都会添加一个新补丁。因此,您需要通过在update
函数中加入以下行来清除以前的补丁。此外,由于要修改轴,因此您希望blit = False。
ax.collections.clear()