我想绘制一个动画,以显示地图上的数据集(ssh)如何随时间变化。或者等效地,我想绘制一个动画,以便每个帧都是basemap.pcolormesh
中的一个图。由于无法上传数据,因此我无法提供完整的代码。该代码的主要部分如下:
import pylab
import matplotlib.animation as animation
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
# map projection
m = Basemap(llcrnrlon=-90.,llcrnrlat=-5.,urcrnrlon=-40.,urcrnrlat=40.,
projection='lcc',lat_1=20.,lat_2=40.,lon_0=-60.,
resolution ='l',area_thresh=1000.)
# plot SSH
x, y = m(lon, lat)
mpc = m.pcolormesh(x, y, ssh[0,:,:], cmap='bwr')
# add colorbar
cbar = m.colorbar(location='bottom', pad="5%")
cbar.set_label('SSH (in meters)', fontsize=20)
# draw coastlines, meridians and parallels.
m.drawcoastlines()
m.drawcountries()
m.drawmapboundary(fill_color='#99ffff')
m.fillcontinents(color='#cc9966',lake_color='#99ffff')
m.drawparallels(np.arange(10,70,20),labels=[1,1,0,0])
m.drawmeridians(np.arange(-100,0,20),labels=[0,0,0,1])
def init():
mpc = m.pcolormesh(x,y,ssh[0,:,:],cmap = 'bwr')
return mpc
def animate(i):
mpc = m.pcolormesh(x,y,ssh[i,:,:],cmap = 'bwr')
return mpc
anim = animation.FuncAnimation(plt.gcf(), animate, init_func=init,
interval=1, blit=True)
plt.show()
如何使其起作用?错误消息如下:
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/cbook/__init__.py", line 388, in process
proxy(*args, **kwargs)
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/cbook/__init__.py", line 228, in __call__
return mtd(*args, **kwargs)
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/animation.py", line 1026, in _start
self._init_draw()
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/animation.py", line 1758, in _init_draw
for a in self._drawn_artists:
TypeError: 'QuadMesh' object is not iterable