def dfAnimate( df, movieName = None, nShaddow = 0, xRatio= 1.0, rate = 1 , xlim = None , ylim = None , xlabel = "x(m)" , ylabel = "Elevation(m)") :
"""
Animate a dataFrame where time is the index, and columns are the "spatial" position
"""
from matplotlib import animation
print ("Making animation file : " , movieName)
global pause
pause = False
def onClick(event):
global pause
pause ^= True
nShaddow = max(1,nShaddow)
fig, ax = plt.subplots()
fig.canvas.mpl_connect('button_press_event', onClick )
ls = []
for i in range(nShaddow) :
if i == 0 :
color = "black"
else :
color = "blue"
ltemp, = ax.plot( [], [], lw=1 , alpha = 1-i*1./nShaddow , color = color)
ls.append(ltemp)
xVal = df.columns
ax.grid(True)
if xlim :
ax.set_xlim( xlim )
else :
ax.set_xlim( min(xVal) , max(xVal) )
if ylim :
ax.set_ylim( ylim )
else :
ax.set_ylim( df.min().min() , df.max().max() )
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
def run(itime):
ax.set_title("{}s".format(df.index[itime*rate]) )
for s in range(nShaddow):
if not pause :
if itime > s :
ls[s].set_data( xVal , df.iloc[ rate*(itime - s) , : ] )
return ls
ani = animation.FuncAnimation( fig , run, range(len(df)), blit=True, interval=30, repeat=False)
if movieName is None :
plt.show()
else :
mywriter = animation.FFMpegWriter(fps = 25 , codec="libx264")
ani.save( movieName +'.mp4' , writer=mywriter)
评论列表
文章目录