import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation fig = plt.figure() x = np.arange(0, 2*np.pi, 0.01) plt.plot(x, np.sin(x),'b') plt.axis([0,2*np.pi,-1,1]) plt.show() def animate_sine(t): plt.plot(x,np.sin(x+t/10.0),'b') plt.axis([0,2*np.pi,-1,1]) fig=plt.figure() ani = animation.FuncAnimation(fig,animate_sine, frames=200) plt.show() def animate_sine(t): plt.cla() plt.plot(x,np.sin(x+t/10.0),'b') plt.axis([0,2*np.pi,-1,1]) fig, ax = plt.subplots() ani = animation.FuncAnimation(fig,animate_sine, frames=200) plt.show() fig,ax = plt.subplots() #An empty plot pl,=ax.plot([],[]) ax.set_xlim([0,2*np.pi]) ax.set_ylim([-1,1]) #animate funcion def animate_sine(t): pl.set_data(x,np.sin(x+t/10.0)) return pl, #set a clear initial frame def init(): pl.set_data([],[]) return pl, #blit=True to remove instances from previous frames ani = animation.FuncAnimation(fig,animate_sine, frames=200, init_func=init, blit=True) plt.show() fig, ax = plt.subplots() ani = animation.FuncAnimation(fig,animate_sine, frames=200) mywriter = animation.FFMpegWriter(fps=50) ani.save('sine_wave.mp4', writer=mywriter) import pandas as pd %matplotlib inline ! cat traj.txt data=pd.read_csv('traj.txt',delimiter=' ',names=['num','lon','lat','depth','age']) print data lon = data['lon'] lat=data['lat'] dep = data['depth'] fig =plt.figure() plt.scatter(lon,lat,color='blue') plt.scatter(lon[0],lat[0],color='red') plt.title('2D') plt.xlabel('Longitude') plt.ylabel('Latitude') plt.axis([-124.6,-123.0,48.25,48.65]) #Empty map fig= plt.figure() plt.title('2D') plt.xlabel('Longitude') plt.ylabel('Latitude') plt.axis([-124.6,-123.0,48.25,48.65]) #Animated points def animate(p): plt.scatter(lon[p],lat[p],color='blue') #The animation function anim = animation.FuncAnimation(fig, animate, frames=10) #A line that makes it all work mywriter = animation.FFMpegWriter() #Save in current folder anim.save('Software-Carpentry2D.mp4',writer=mywriter) from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax=plt.gca(projection='3d') ax.scatter(lon,lat,dep,color='blue') ax.set_title('3D') ax.set_xlabel('Longitude') ax.set_ylabel('Latitude') ax.set_zlabel('Depth') ax.set_xlim([-124.6,-123.0]) ax.set_ylim([48.25,48.65]) ax.set_zlim([-100,0]) fig = plt.figure() ax=plt.gca(projection='3d') ax.scatter(lon,lat,dep,color='blue') ax.set_title('3D') ax.set_xlabel('Longitude') ax.set_ylabel('Latitude') ax.set_zlabel('Depth') ax.set_xlim([-124.6,-123.0]) ax.set_ylim([48.25,48.65]) ax.set_zlim([-100,0]) ax.view_init(elev=0, azim=-90) #Empty map fig = plt.figure(figsize=(20,10)) ax = fig.gca(projection='3d') ax.set_title('3D') ax.set_xlabel('Longitude') ax.set_ylabel('Latitude') ax.set_zlabel('Depth') ax.set_xlim([-124.6,-123.0]) ax.set_ylim([48.25,48.65]) ax.set_zlim([-100,0]) ax.view_init(elev=0, azim=-90) #Animated points def animate(p): ax.scatter(lon[p],lat[p],dep[p],color='blue') #The animation function anim = animation.FuncAnimation(fig, animate, frames=10) #A line that makes it all work mywriter = animation.FFMpegWriter() #Save in current folder anim.save('Software-Carpentry3D.mp4',writer=mywriter)