简体   繁体   English

如何在底图上使用Matplotlib制作动画子图?

[英]How to make an animation subplot with Matplotlib on Basemap?

I'm trying to make a two-panel animation with time. 我正在尝试制作带有时间的两面板动画。 I've found a similar example that I want to do, but I cannot go through with. 我找到了一个我想做的类似示例 ,但我无法通过。 Here's my 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, ax = plt.subplots(1,2)

ax[0].set_title("aaaa")
m_a = 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_a.drawparallels(np.arange(-90.,99.,30.), labels=[1,0,0,0])
m_a.drawmeridians(np.arange(-180.,180.,60.), labels=[0,0,0,1])
m_a.drawcoastlines(linewidth=0.25)
m_a.pcolormesh(lons,lats,y[0],cmap=plt.cm.bwr, shading='flat',latlon=True)

ax[1].set_title("bbbb")
m_b = 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_b.drawparallels(np.arange(-90.,99.,30.), labels=[1,0,0,0])
m_b.drawmeridians(np.arange(-180.,180.,60.), labels=[0,0,0,1])
m_b.drawcoastlines(linewidth=0.25)
m_b.pcolormesh(lons,lats,y[0],cmap=plt.cm.bwr, shading='flat',latlon=True)


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

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

plt.show()

However, the result animation is here⇒ enter image description here . 但是,结果动画在这里在此处输入图像描述 Why is this happening? 为什么会这样呢? I'm glad if you review the code. 如果您查看代码,我感到很高兴。

I guess you just forgot to specify the axes or the basemap to live in. 我猜您只是忘了指定要居住的轴或底图。

m_a = Basemap(projection='kav7',lon_0=0, ax=ax[0])
# ...
m_b = Basemap(projection='kav7',lon_0=0, ax=ax[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM