2015-07-01 40 views
0

我通过matplotlib的底图绘制2D数组并绘制时间动画。然而,我在添加时间计数器时遇到了麻烦。动画从0 UT开始,在23UT处开始,然后从0UT开始。动画效果非常好。在Matplotlib底图中设置动画文本

这里是我的代码:

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, 

但是这只会导致整个动画停止。我一直在试图弄清楚这个问题好几个星期。请借我你的天才几秒钟!谢谢!

回答

0

简单的解决方案,但它不能在任何地方明确找到。只需将文本实例从figtext更改为简单的ax.text实例,并且动画完美无缺。