2015-07-01 37 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實例,並且動畫完美無缺。