带底图的 Matplotlib 子图动画

Matplotlib Subplot Animation with Basemap

我正在尝试生成温度随时间变化的四面板动画。子图中的四个面板中的每一个都应该是动画地图;每个面板之间的差异是所使用的数据。我已经设法使用一组数据(没有子图)生成动画,代码如下:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.basemap import Basemap

#dummy temperature data with 10 time-steps
y=np.random.randn(10, 60, 100) 

fig = plt.figure()
m = Basemap(projection='kav7',lon_0=0)
lats=np.linspace(90,-90,y.shape[1])
lons=np.linspace(-180,180,y.shape[2])
lons, lats = np.meshgrid(lons,lats)

m.drawparallels(np.arange(-90.,99.,30.), labels=[1,0,0,0])
m.drawmeridians(np.arange(-180.,180.,60.), labels=[0,0,0,1])
m.drawcoastlines(linewidth=0.25)
m.pcolormesh(lons,lats,y[0],cmap=plt.cm.bwr, shading='flat',latlon=True)

def init():
    m                                                                                                                                                                                                                                                                                                 

def animate(i):
    m.pcolormesh(lons,lats,y[i],cmap=plt.cm.bwr, shading='flat',latlon=True)
    return m

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=10, interval=100) #interval = number of milliseconds between frames                                                                                                                                                                                                                                                                                                                       

anim.save('movie.mp4')

我看过很多子图动画的例子 (1, 2, 3),但仍然不知道如何使用 Basemap 来实现。

您的动画函数需要更新图中四个独立轴上的四个不同地图。

  • 创建四个面板:

    fig, ax = plt.subplots(2, 2)
    

轴已编入索引。我建议将它们放置在 2x2 布局中,它们被索引为二维数组,因此将轴引用为 ax[0][0]ax[0][1]ax[1][0]ax[1][1]创建地图时。

  • 当您创建四个底图实例时,使用 ax参数(docs).

  • 在您的 animate 函数中,更改每个坐标轴的颜色。如您的问题所示,根据数据为地图 m_i 分配颜色 m_i.pcolormesh (docs)