使用Basemap制作Matplotlib子图动画

4

我正在尝试生成一个随时间变化的四面板温度动画。子图中的四个面板应该是动态地显示地图;每个面板之间的差异在于使用的数据不同。我已经使用以下代码生成了一个数据集(不带子图)的动画:

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

我查看了许多subplot动画的例子 (1, 2, 3),但仍然不知道如何使用Basemap来完成。


如果您提供了您所参考的众多示例之一的链接,那么这将是一个更好的问题。http://matplotlib.org/api/animation_api.html?highlight=funcanimation#matplotlib.animation.FuncAnimation - Bennett Brown
感谢您在示例中进行编辑!并且接受了一个答案。欢迎来到SO! - Bennett Brown
1个回答

2

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

  • Create your four panels:

    fig, ax = plt.subplots(2, 2)
    
轴是有索引的。我建议将它们放置在2x2布局中,它们被索引为2维数组,因此在创建地图时,请将轴引用为ax [0] [0]ax [0] [1]ax [1] [0]ax [1] [1]
  • 当您创建四个Basemap实例时,请使用ax参数将每个地图分配给不同的轴(docs)

  • 在您的animate函数中,更改每个轴的着色。根据数据映射m_i来分配颜色可以使用m_i.pcolormesh(docs),如您在问题中所示。


1
谢谢BBrown。在你的帮助下,我已经解决了它。对于一个双面板图,还需要调整“init”函数: def init(): fig并调整动画函数为: def animate(i): m_a.pcolormesh(lons,lats,y [i],cmap = plt.cm.bwr,shading ='flat',latlon = True) m_b.pcolormesh(lons,lats,y [i],cmap = plt.cm.bwr,shading ='flat',latlon = True) 返回m_a,m_b - Oliver Angelil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接