Matplotlib 底图中的动画文本
Animating Text in Matplotlib Basemap
我在 matplotlib 的底图上绘制了一个二维数组,并随时间对其进行动画处理。但是我在添加时间计数器时遇到了麻烦。动画从 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,
但这只会导致整个动画停止。几个星期以来,我一直在努力解决这个问题。请把你的天才借给我几秒钟!谢谢!
简单的解决方案,但无法在任何地方明确找到。只需将文本实例从图形文本更改为简单的 ax.text 实例,动画就可以完美运行。
我在 matplotlib 的底图上绘制了一个二维数组,并随时间对其进行动画处理。但是我在添加时间计数器时遇到了麻烦。动画从 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,
但这只会导致整个动画停止。几个星期以来,我一直在努力解决这个问题。请把你的天才借给我几秒钟!谢谢!
简单的解决方案,但无法在任何地方明确找到。只需将文本实例从图形文本更改为简单的 ax.text 实例,动画就可以完美运行。