首页 文章

使用底图Python的Contourf和箭头动画

提问于
浏览
3

我有两个不同的数据集,在公共区域上有不同的(lat,lon)网格 . 我试图在一个共同的底图上绘制一个的contourf和另一个的quiver,然后随着时间的推移动画这个 . 我跟着这个http://matplotlib.org/basemap/users/examples.htmlhttps://github.com/matplotlib/basemap/blob/master/examples/animate.py .

到目前为止我有:

m = Basemap(llcrnrlon=min(lon),llcrnrlat=min(lat),urcrnrlon=max(lon),urcrnrlat=max(lat),
            rsphere=(6378137.00,6356752.3142),resolution='h',projection='merc')

# first dataset
lons, lats = numpy.meshgrid(lon, lat)
X, Y = m(lons, lats)

# second dataset
lons2, lats2 = numpy.meshgrid(lon2, lat2)
xx, yy = m(lons2, lats2)

#colormap 
levels = numpy.arange(0,3,0.1)
cmap = plt.cm.get_cmap("gist_rainbow_r")

# create figure.
fig=plt.figure(figsize=(12,8))
ax = fig.add_axes([0.05,0.05,0.8,0.85])

# contourf 
i = 0
CS = m.contourf(xx,yy,AUX[i,:,:],levels,cmap=cmap,extend='max')
cbar=plt.colorbar(CS)

# quiver
x = X[0::stp,0::stp]   #plot arrows with stp = 2
y = Y[0::stp,0::stp]
uplt = U[i,0::stp,0::stp]
vplt = V[i,0::stp,0::stp]
Q = m.quiver(x,y,uplt,vplt,color='k',scale=15)
qk = ax.quiverkey(Q,0.1,0.1,0.5,'0.5m/s')

# continents 
m.drawcoastlines(linewidth=1.25)
m.fillcontinents(color='0.8')

def updatefig(i):
    global CS, Q
    for c in CS.collections: c.remove()

    CS = m.contourf(xx,yy,AUX[i,:,:],levels,cmap=cmap,extend='max')

    uplt = U[i,0::stp,0::stp]
    vplt = V[i,0::stp,0::stp]
    Q.set_UVC(uplt,vplt)

anim = animation.FuncAnimation(fig, updatefig, frames=AUX.shape[0],blit=False)

plt.show()

对于第一个绘图(i = 0),一切正常,但之后我只得到了没有任何箭头图叠加的contourf动画(但是quiverkey出现了!)两个动画分别工作正常,但不能在一起 . 是否存在底图上有两个不同的x,y的问题?

2 回答

  • 0

    我能够通过在函数内添加箭头图并在保存图之后添加Q.remove()来解决这个问题 . 最终结果如下:

    def updatefig(i):
        global CS, Q
        for c in CS.collections: c.remove()
    
        CS = m.contourf(xx,yy,AUX[i,:,:],levels,cmap=cmap,extend='max')
    
        uplt = U[i,0::stp,0::stp]
        vplt = V[i,0::stp,0::stp]
        Q = m.quiver(x,y,uplt,vplt,color='k',scale=15)
    
        # SAVE THE FIGURE
        Q.remove()  #after saving the figure
    
     anim = animation.FuncAnimation(fig, updatefig, frames=AUX.shape[0],blit=False)
    
    plt.show()
    

    虽然我仍然找不到答案,但是set_UVC()与contourf不兼容...

  • 0

    您可以在绘制第二部分(箭头)之前尝试 ax.autoscale(False) .
    希望它会有所帮助

相关问题