具有底图的Matplotlib子图动画 [英] Matplotlib Subplot Animation with Basemap

查看:79
本文介绍了具有底图的Matplotlib子图动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个四面板的动画,显示温度随时间的变化.子图中的四个面板中的每个面板均应为动画地图.每个面板之间的差异就是所使用的数据.我设法使用一组数据(没有子图)通过以下代码生成动画:

I am trying to generate a four-panel animation of temperature change with time. Each of the four panels in the subplot should be an animated map; the difference between each panel being the data used. I have managed to generate the animation using one set of data (without subplots) with the following code:

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 3 ),但是仍然不知道如何使用底图进行动画处理.

I have looked at numerous examples (1, 2, 3) of subplot animations, but still have no idea how to go about doing it with Basemap.

推荐答案

您的动画功能需要在图中的四个独立轴上更新四个不同的贴图.

Your animation function needs to update four different maps in four separate axes within the figure.

  • 创建四个面板:

  • Create your four panels:

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

索引轴.我建议将它们放置在2x2布局中的方式是将它们索引为2维数组,因此在创建地图时将轴引用为ax[0][0]ax[0][1]ax[1][0]ax[1][1].

The axes are indexed. The way I suggested placing them in a 2x2 layout, they are indexed as a 2-d array, so reference the axes as ax[0][0], ax[0][1], ax[1][0], and ax[1][1] when you create the maps.

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

  • When you create your four instances of Basemap, assign each map to a different axes using the ax parameter (docs).

在您的animate功能中,更改每个轴的颜色.使用m_i.pcolormesh 根据数据为地图m_i分配颜色(文档),如您的问题所示.

In your animate function, change the coloring of each axes. Assigning colors based on data to map m_i is with m_i.pcolormesh (docs) as shown in your question.

这篇关于具有底图的Matplotlib子图动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆