Python 在Matplotlib底图中设置文本动画

Python 在Matplotlib底图中设置文本动画,python,animation,matplotlib,matplotlib-basemap,Python,Animation,Matplotlib,Matplotlib Basemap,我在matplotlib的底图上绘制了一个2D数组,并通过时间为其设置动画。不过,我在添加计时器时遇到了问题。动画从0 UT开始,经过23 UT,然后在0 UT重新开始。动画效果很好 这是我的密码: from netCDF4 import Dataset import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap, addcyclic import matplotlib.a

我在matplotlib的底图上绘制了一个2D数组,并通过时间为其设置动画。不过,我在添加计时器时遇到了问题。动画从0 UT开始,经过23 UT,然后在0 UT重新开始。动画效果很好

这是我的密码:

from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, addcyclic
import matplotlib.animation as animation

#load the netcdf file into a variable
mar120="C:/Users/WillEvo/Desktop/My Datasets To Share/SA120_Iono_Acc_WE.nc"

#grab the data into a new variable
fh=Dataset(mar120,mode="r")

#assign model variable contents to python variables
lons=fh.variables['lon'][:]
lats=fh.variables['lat'][:]
var1=fh.variables['NTD_UP'][:]

#specifying which time and elevation to map
ionst=var1[0,18,:,:]
details='(0UT, Z=2)'

#close the netCDF file
fh.close()

details='(0UT, Z=2)'

# get rid of white stripe on map
ionst, lons=addcyclic(ionst, lons)

#Setting figure attributes
fig=plt.figure(figsize=(12,12),facecolor='white')

#map settings
m=Basemap(llcrnrlon=-180, llcrnrlat=-87.5, urcrnrlon=180, urcrnrlat=87.5,rsphere=6467997, resolution='l', projection='cyl',area_thresh=10000, lat_0=0, lon_0=0)

#Creating 2d array of latitude and longitude
lon, lat=np.meshgrid(lons, lats)
xi, yi=m(lon, lat)

#plotting data onto basemap
cs=m.imshow(ionst, interpolation=None, alpha=.8)

#drawing grid lines
m.drawparallels(np.arange(-90.,90.,30.),labels=[1,0,0,0],fontsize=10)
m.drawmeridians(np.arange(-180.,181.,30.), labels=[0,0,0,1],fontsize=10)

#drawing coast lines
m.drawcoastlines()

#color bar
cbar=m.colorbar(cs, location='bottom', pad="10%")
cbar.set_label(r"Ion Drag $(cm/s^2)$")

#Title Preferences
plt.title('Ion Drag at '+details, size=16)


#Function to update the plots data
def updateax1(j):
    cs.set_array(var1[j,18,:,:])
    return cs,

#Animate the plot
ani1=animation.FuncAnimation(fig, updateax1, frames=range(24), interval=150, blit=True)

#showing the plot
plt.show()
我尝试过这样设置图形文本:

text=figtext(0,0,'sometext')
def updateax1(j):
        text.set_text(str(j))
        cs.set_array(var1[j,18,:,:])
        return cs, text,
然后在我的更新函数中更新它,如下所示:

text=figtext(0,0,'sometext')
def updateax1(j):
        text.set_text(str(j))
        cs.set_array(var1[j,18,:,:])
        return cs, text,

但这只会导致整个动画停止。几个星期来我一直在想这个问题。请把你的天才借给我几秒钟!谢谢

简单的解决方案,但在任何地方都找不到。只需将文本实例从figtext更改为简单的ax.text实例,动画就可以完美地工作