如何正确地随着时间的推移制作动画。静态背景图(cartopy)+动态轮廓+动态标题

how to animate over time properly. static background map (cartopy) + dynamic contourf + dynamic title

我一直在尝试创建地图上浓度的 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,LAT, Z[:,:,i],
            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()
            )
        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表示不刷新每一步的信息。在每一步中,旧帧的先前表示仍然存在。因此,最终,最后一帧的结果等于 4 个表示帧的重叠。

如何在不丢失背景地图的情况下更改此行为?我只想在每一帧中显示 Z[:,:,i]。

我最后一帧的结果图是这样的:

提前致谢!

一系列补丁存储在集合中。每次调用更新时都会添加一个新补丁。因此,您需要通过在 update 函数中加入以下行来清除以前的补丁。此外,您希望 blit=False 因为正在修改轴。

ax.collections.clear()