Matplotlib底图中的动画文本

时间:2015-07-01 23:01:06

标签: python animation matplotlib matplotlib-basemap

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

这是我的代码:

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,

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

1 个答案:

答案 0 :(得分:0)

简单的解决方案但是在任何地方都无法明确地找到它。只需将文本实例从figtext更改为简单的ax.text实例,动画就可以完美运行。