Matplotlib Subplot Animation with Basemap

元气小坏坏 提交于 2019-12-10 15:43:11

问题


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

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.


回答1:


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)
    

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.

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

  • 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.



来源:https://stackoverflow.com/questions/33458488/matplotlib-subplot-animation-with-basemap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!