用Basemap Python进行轮廓和抖动动画

时间:2022-07-18 14:59:50

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.

我有两个不同的数据集,不同的(lat, lon)网格在一个公共区域。我试着在一个常见的basemap上画出一个和另一个的一个contourf,然后随着时间的推移动画。我遵循了http://matplotlib.org/basemap/users/examples.html和这个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?

对于第一个情节(i=0),一切都很好,但之后我只得到轮廓线动画,没有任何颤抖情节叠加(但quiverkey出现了!)两个动画分别工作得很好,但不是一起工作。在基底上有两个不同的x和y有问题吗?

2 个解决方案

#1


0  

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

在绘制第二部分(quiver)之前,可以尝试使用ax.autoscale(False)。希望会对您有所帮助

#2


0  

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:

我可以通过在函数中添加抖动图并在保存该图之后添加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()

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

虽然我仍然无法找到我set_UVC()不能与contourf一起工作的答案,但它的工作方式与我的预期一致。

#1


0  

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

在绘制第二部分(quiver)之前,可以尝试使用ax.autoscale(False)。希望会对您有所帮助

#2


0  

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:

我可以通过在函数中添加抖动图并在保存该图之后添加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()

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

虽然我仍然无法找到我set_UVC()不能与contourf一起工作的答案,但它的工作方式与我的预期一致。