Matplotlib 如何随时间正确设置动画。静态背景图(cartopy)和#x2B;动态轮廓f+;动态标题

Matplotlib 如何随时间正确设置动画。静态背景图(cartopy)和#x2B;动态轮廓f+;动态标题,matplotlib,animation,title,cartopy,contourf,Matplotlib,Animation,Title,Cartopy,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(dra

我一直在尝试创建地图上浓度的轮廓表示动画,我使用的主要代码是:

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()

这解决了我使用python终端时的问题。您知道如何在vscode(我使用的)中将其应用于jupiter笔记本或Python交互控制台吗???