4

I have two different datasets with different (lat, lon) grid over a common region. I am trying to plot a contourf of one and quiver of another on a common basemap, and then animate this over time. I have followed this http://matplotlib.org/basemap/users/examples.html and this https://github.com/matplotlib/basemap/blob/master/examples/animate.py.

So far I have:

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

Everything works fine for the first plot (i=0) but afterwards I only get the contourf animation without any quiver plot superimposed (but the quiverkey appears!) Both animations separately work fine, but not together. Is there a problem of having two different x,y on a basemap?

2 Answers2

1

I was able to work it out by adding the quiver plot inside the function and adding a Q.remove() after saving the plot. It ended with something like:

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

It works like I intended although I still can't find the answer I set_UVC() does not work with contourf...

0

You can try ax.autoscale(False) before you plot the second part(quiver).
Hope it'll be helpful

Han Zhengzu
  • 3,694
  • 7
  • 44
  • 94
  • Thank you for your reply...adding ax.autoscale(False) did not work. I was able to resolve this by adding Q=m.quiver(x,y,uplt,vplt,color='k',scale=15) inside the updatefig function, however the plotting is really slow. I think every plot is overlayed on the previous one over the loop and only works because the contourf "hides" the previous plot so I am still trying to work on a better solution! –  Jan 04 '16 at 13:58