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

时间:2018-06-21 00:51:48

标签: python animation matplotlib matplotlib-basemap

我正在尝试制作带有时间的两面板动画。我已经找到了类似的example,但我无法接受。这是我的代码。

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

但是,结果动画在这里⇒enter image description here。为什么会这样呢?如果您查看代码,我感到很高兴。

1 个答案:

答案 0 :(得分:0)

我想您只是忘了指定要居住的轴或底图。

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