【问题标题】:Matplotlib Subplot Animation with BasemapMatplotlib 子图动画与底图
【发布时间】:2016-02-01 04:58:17
【问题描述】:

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

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

我看过很多次情节动画示例(123),但仍然不知道如何使用 Basemap 进行操作。

【问题讨论】:

  • 如果您链接到您提到的众多示例之一,那将是一个更好的问题。 matplotlib.org/api/…
  • 感谢您编辑示例!并接受答案。欢迎来到 SO!

标签: python animation matplotlib matplotlib-basemap subplot


【解决方案1】:

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

  • 创建您的四个面板:

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

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

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

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

【讨论】:

  • 谢谢 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) return m_a, m_b
猜你喜欢
  • 1970-01-01
  • 2015-09-19
  • 2018-01-08
  • 2015-07-02
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
相关资源
最近更新 更多