Professional Documents
Culture Documents
Pep8ify - Examples - Animation 99d3477 Matplotlib - Matplotlib GitHubjh
Pep8ify - Examples - Animation 99d3477 Matplotlib - Matplotlib GitHubjh
pep8ify:examples/animation99d3477matplotlib/matplotlibGitHub
Thisrepository
Search
Explore
Features
matplotlib / matplotlib
Enterprise
Blog
Star
pep8ify:examples/animation
Signup
2,044
Signin
Fork
Browsecode
985
Signedoffby:ThomasHisch<t.hisch@gmail.com>
master(#3425)
thischauthoredonAug27
1parent9661a02
Showing16changedfileswith102additionsand75deletions.
9 examples/animation/animate_decay.py
2
3
4
importmatplotlib.pyplotasplt
importmatplotlib.animationasanimation
5
6
5
6
7
+
defdata_gen():
7
8
8
9
9
10
11
12
13
18
18
19
19
20
20
21
22
23
data_gen.t=0
ax.set_xlim(0,5)
ax.grid()
xdata,ydata=[],[]
+
+
23
t,y=data
+t,y=data
xdata.append(t)
24
25
27
28
ydata.append(y)
xmin,xmax=ax.get_xlim()
@@32,5+35,5@@defrun(data):
35
36
37
38
39
returnline,
ani=animation.FuncAnimation(fig,run,data_gen,blit=True,interval=10,
repeat=False)
+repeat=False)
plt.show()
9 examples/animation/basic_example.py
7
8
9
10
7
8
9
View
@@2,8+2,9@@
2
3
4
5
6
5
6
t+=0.05
yieldt,np.sin(2*np.pi*t)*np.exp(t/10.)
25
26
2
3
4
View
whilecnt<1000:
cnt+=1
+cnt+=1
defrun(data):
#updatethedata
36
t=data_gen.t
cnt=0
24
32
33
34
35
Split
@@17,9+18,11@@defdata_gen():
17
21
22
Unified
@@2,11+2,12@@
2
3
4
10
11
12
commit99d3477341f63d802aaf58446c56241fd9869c4f
importmatplotlib.pyplotasplt
importmatplotlib.animationasanimation
+
defupdate_line(num,data,line):
line.set_data(data[...,:num])
+line.set_data(data[...,:num])
returnline,
fig1=plt.figure()
@@15,8+16,8@@defupdate_line(num,data,line):
https://github.com/matplotlib/matplotlib/commit/99d3477341f63d802aaf58446c56241fd9869c4f
1/9
11/23/2014
pep8ify:examples/animation99d3477matplotlib/matplotlibGitHub
15
16
16
17
17
18
plt.xlabel('x')
plt.title('test')
line_ani=animation.FuncAnimation(fig1,update_line,25,fargs=(data,l),
interval=50,blit=True)
#line_ani.save('lines.mp4')
18
19
19
20
+interval=50,blit=True)
20
21
21
22
22
23
fig2=plt.figure()
+#line_ani.save('lines.mp4')
@@28,7+29,7@@defupdate_line(num,data,line):
28
29
ims.append((plt.pcolor(x,y,base+add,norm=plt.Normalize(0,30)),))
29
30
30
31
31
32
32
33
33
34
34
35
im_ani=animation.ArtistAnimation(fig2,ims,interval=50,repeat_delay=3000,
blit=True)
+blit=True)
#im_ani.save('im.mp4',metadata={'artist':'Guido'})
plt.show()
7 examples/animation/basic_example_writer.py
@@7,8+7,9@@
importmatplotlib.pyplotasplt
8
9
8
9
importmatplotlib.animationasanimation
10
10
11
11
12
12
13
13
14
14
15
+
defupdate_line(num,data,line):
line.set_data(data[...,:num])
+line.set_data(data[...,:num])
returnline,
#Setupformattingforthemoviefiles
@@25,7+26,7@@defupdate_line(num,data,line):
25
26
plt.xlabel('x')
26
27
28
27
28
plt.title('test')
line_ani=animation.FuncAnimation(fig1,update_line,25,fargs=(data,l),
29
30
29
30
31
31
32
interval=50,blit=True)
+interval=50,blit=True)
line_ani.save('lines.mp4',writer=writer)
fig2=plt.figure()
@@38,5+39,5@@defupdate_line(num,data,line):
38
39
39
40
40
41
41
42
42
43
ims.append((plt.pcolor(x,y,base+add,norm=plt.Normalize(0,30)),))
im_ani=animation.ArtistAnimation(fig2,ims,interval=50,repeat_delay=3000,
blit=True)
+blit=True)
im_ani.save('im.mp4',writer=writer)
4 examples/animation/bayes_update.py
View
@@4,7+4,9@@
importscipy.statsasss
5
6
5
6
7
frommatplotlib.animationimportFuncAnimation
7
8
8
9
10
9
10
11
12
+
classUpdateDist(object):
+
def__init__(self,ax,prob=0.5):
self.success=0
self.prob=prob
@@43,5+45,5@@def__call__(self,i):
43
45
44
45
46
46
47
ax=fig.add_subplot(1,1,1)
ud=UpdateDist(ax,prob=0.7)
anim=FuncAnimation(fig,ud,frames=np.arange(100),init_func=ud.init,
48
49
interval=100,blit=True)
+interval=100,blit=True)
plt.show()
47
View
https://github.com/matplotlib/matplotlib/commit/99d3477341f63d802aaf58446c56241fd9869c4f
2/9
11/23/2014
pep8ify:examples/animation99d3477matplotlib/matplotlibGitHub
28 examples/animation/double_pendulum_animated.py
@@7,11+7,11@@
7
8
7
8
9
10
11
importscipy.integrateasintegrate
importmatplotlib.animationasanimation
G=9.8#accelerationduetogravity,inm/s^2
L1=1.0#lengthofpendulum1inm
L2=1.0#lengthofpendulum2inm
M1=1.0#massofpendulum1inkg
12
13
14
10
11
12
15
13
14
15
16
17
16
17
M2=1.0#massofpendulum2inkg
+G=9.8#accelerationduetogravity,inm/s^2
+L1=1.0#lengthofpendulum1inm
+L2=1.0#lengthofpendulum2inm
+M1=1.0#massofpendulum1inkg
+M2=1.0#massofpendulum2inkg
defderivs(state,t):
@@46,19+46,17@@defderivs(state,t):
46
46
47
48
49
47
48
th2=10.0
w2=0.0
rad=pi/180
50
51
49
52
53
50
51
54
55
56
52
53
54
57
58
55
56
57
59
60
#initialstate
state=np.array([th1,w1,th2,w2])*pi/180.
+state=np.radians([th1,w1,th2,w2])
#integrateyourODEusingscipy.integrate.
y=integrate.odeint(derivs,state,t)
x1=L1*sin(y[:,0])
y1=L1*cos(y[:,0])
+x1=L1*sin(y[:,0])
+y1=L1*cos(y[:,0])
x2=L2*sin(y[:,2])+x1
y2=L2*cos(y[:,2])+y1
61
58
62
63
59
60
61
64
62
+x2=L2*sin(y[:,2])+x1
+y2=L2*cos(y[:,2])+y1
fig=plt.figure()
ax=fig.add_subplot(111,autoscale_on=False,xlim=(2,2),ylim=(2,2))
@@68,21+66,23@@defderivs(state,t):
68
69
66
67
time_template='time=%.1fs'
time_text=ax.text(0.05,0.9,'',transform=ax.transAxes)
70
71
68
69
70
72
73
71
72
definit():
line.set_data([],[])
time_text.set_text('')
74
73
returnline,time_text
75
74
75
76
77
78
79
80
76
77
78
79
80
defanimate(i):
thisx=[0,x1[i],x2[i]]
thisy=[0,y1[i],y2[i]]
81
82
83
84
line.set_data(thisx,thisy)
time_text.set_text(time_template%(i*dt))
+time_text.set_text(time_template%(i*dt))
returnline,time_text
ani=animation.FuncAnimation(fig,animate,np.arange(1,len(y)),
85
86
87
88
interval=25,blit=True,init_func=init)
+interval=25,blit=True,init_func=init)
#ani.save('double_pendulum.mp4',fps=15)
plt.show()
81
82
83
84
85
86
87
88
View
https://github.com/matplotlib/matplotlib/commit/99d3477341f63d802aaf58446c56241fd9869c4f
3/9
11/23/2014
pep8ify:examples/animation99d3477matplotlib/matplotlibGitHub
6 examples/animation/dynamic_image.py
@@8,6+8,7@@
8
9
10
8
9
10
fig=plt.figure()
11
12
13
11
12
13
14
+
deff(x,y):
returnnp.sin(x)+np.cos(y)
@@16,11+17,12@@deff(x,y):
16
17
17
18
im=plt.imshow(f(x,y),cmap=plt.get_cmap('jet'))
18
19
20
21
+
defupdatefig(*args):
globalx,y
+globalx,y
19
20
22
23
21
22
23
24
25
26
27
28
24
25
26
View
x+=np.pi/15.
y+=np.pi/20.
im.set_array(f(x,y))
+im.set_array(f(x,y))
returnim,
ani=animation.FuncAnimation(fig,updatefig,interval=50,blit=True)
5 examples/animation/dynamic_image2.py
@@8,6+8,7@@
8
9
8
9
10
10
11
12
13
14
11
12
13
fig=plt.figure()
+
deff(x,y):
returnnp.sin(x)+np.cos(y)
@@24,9+25,9@@deff(x,y):
24
25
ims.append([im])
25
26
27
26
27
28
29
ani=animation.ArtistAnimation(fig,ims,interval=50,blit=True,
repeat_delay=1000)
+repeat_delay=1000)
30
31
32
33
#ani.save('dynamic_images.mp4')
+#ani.save('dynamic_images.mp4')
plt.show()
28
29
30
31
32
26 examples/animation/histogram.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
View
View
@@28,34+28,36@@
28
29
30
#foreachrect:1fortheMOVETO,3fortheLINETO,1forthe
#CLOSEPOLY;thevertfortheclosepolyisignoredbutwestillneed
#ittokeepthecodesalignedwiththevertices
31
32
33
34
35
nverts=nrects*(1+3+1)
+nverts=nrects*(1+3+1)
verts=np.zeros((nverts,2))
codes=np.ones(nverts,int)*path.Path.LINETO
codes[0::5]=path.Path.MOVETO
codes[4::5]=path.Path.CLOSEPOLY
verts[0::5,0]=left
verts[0::5,1]=bottom
verts[1::5,0]=left
verts[1::5,1]=top
verts[2::5,0]=right
verts[2::5,1]=top
verts[3::5,0]=right
verts[3::5,1]=bottom
https://github.com/matplotlib/matplotlib/commit/99d3477341f63d802aaf58446c56241fd9869c4f
4/9
11/23/2014
pep8ify:examples/animation99d3477matplotlib/matplotlibGitHub
36
37
38
39
40
41
+verts[0::5,0]=left
+verts[0::5,1]=bottom
+verts[1::5,0]=left
+verts[1::5,1]=top
+verts[2::5,0]=right
42
+verts[3::5,0]=right
+verts[3::5,1]=bottom
barpath=path.Path(verts,codes)
patch=patches.PathPatch(barpath,facecolor='green',edgecolor='yellow',alpha=0.5)
43
44
45
44
45
46
46
47
48
49
50
51
47
48
49
50
51
52
52
53
54
55
56
53
54
55
56
57
58
57
58
59
59
60
61
60
61
62
63
+verts[2::5,1]=top
+patch=patches.PathPatch(
+barpath,facecolor='green',edgecolor='yellow',alpha=0.5)
ax.add_patch(patch)
ax.set_xlim(left[0],right[1])
ax.set_ylim(bottom.min(),top.max())
+
defanimate(i):
#simulatenewdatacomingin
data=np.random.randn(1000)
n,bins=np.histogram(data,100)
top=bottom+n
verts[1::5,1]=top
verts[2::5,1]=top
+verts[1::5,1]=top
+verts[2::5,1]=top
ani=animation.FuncAnimation(fig,animate,100,repeat=False)
plt.show()
5 examples/animation/moviewriter.py
12
13
14
15
@@12,7+12,7@@
12
13
14
16
15
16
17
18
17
18
21
22
23
24
25
26
27
FFMpegWriter=manimation.writers['ffmpeg']
metadata=dict(title='MovieTest',artist='Matplotlib',
comment='Moviesupport!')
+comment='Moviesupport!')
writer=FFMpegWriter(fps=15,metadata=metadata)
fig=plt.figure()
@@21,7+21,7@@
21
22
23
plt.xlim(5,5)
plt.ylim(5,5)
24
25
26
27
x0,y0=0,0
+x0,y0=0,0
withwriter.saving(fig,"writer_test.mp4",100):
foriinrange(100):
@@29,4+29,3@@
29
29
y0+=0.1*np.random.randn()
30
31
32
30
31
l.set_data(x0,y0)
writer.grab_frame()
16 examples/animation/rain.py
8
9
10
11
12
13
14
15
View
View
@@8,14+8,14@@
8
9
10
11
12
13
"""
importnumpyasnp
importmatplotlib.pyplotasplt
frommatplotlib.animationimportFuncAnimation
+frommatplotlib.animationimportFuncAnimation
#CreatenewFigureandanAxeswhichfillsit.
fig=plt.figure(figsize=(7,7))
https://github.com/matplotlib/matplotlib/commit/99d3477341f63d802aaf58446c56241fd9869c4f
5/9
11/23/2014
pep8ify:examples/animation99d3477matplotlib/matplotlibGitHub
14
15
16
16
17
18
19
20
17
18
19
20
21
21
31
32
33
34
35
36
37
#Createraindata
n_drops=50
@@31,7+31,7@@
#Constructthescatterwhichwewillupdateduringanimation
#astheraindropsdevelop.
scat=ax.scatter(rain_drops['position'][:,0],rain_drops['position'][:,1],
34
+scat=ax.scatter(rain_drops['position'][:,0],rain_drops['position'][:,1],
s=rain_drops['size'],lw=0.5,edgecolors=rain_drops['color'],
facecolors='none')
@@42,7+42,7@@defupdate(frame_number):
42
43
44
45
45
46
47
48
46
47
48
ax.set_xlim(0,1),ax.set_xticks([])
ax.set_ylim(0,1),ax.set_yticks([])
+ax.set_xlim(0,1),ax.set_xticks([])
+ax.set_ylim(0,1),ax.set_yticks([])
31
32
33
35
36
37
42
43
44
+#CreatenewFigureandanAxeswhichfillsit.
+fig=plt.figure(figsize=(7,7))
ax=fig.add_axes([0,0,1,1],frameon=False)
#Makeallcolorsmoretransparentastimeprogresses.
rain_drops['color'][:,3]=1.0/len(rain_drops)
rain_drops['color'][:,3]=np.clip(rain_drops['color'][:,3],0,1)
+rain_drops['color'][:,3]=np.clip(rain_drops['color'][:,3],0,1)
#Makeallcirclesbigger.
rain_drops['size']+=rain_drops['growth']
@@58,7+58,7@@defupdate(frame_number):
58
58
59
60
59
60
scat.set_edgecolors(rain_drops['color'])
scat.set_sizes(rain_drops['size'])
scat.set_offsets(rain_drops['position'])
61
62
63
64
#Constructtheanimation,usingtheupdatefunctionastheanimation
#director.
61
62
63
64
5 examples/animation/random_data.py
@@6,12+6,15@@
line,=ax.plot(np.random.rand(10))
7
8
7
8
ax.set_ylim(0,1)
9
10
+
defupdate(data):
10
11
11
12
line.set_ydata(data)
12
13
returnline,
13
14
15
+
defdata_gen():
whileTrue:yieldnp.random.rand(10)
14
17
+whileTrue:
+yieldnp.random.rand(10)
15
16
18
19
ani=animation.FuncAnimation(fig,update,data_gen,interval=100)
17
20
plt.show()
16
14 examples/animation/simple_3danim.py
View
@@6,7+6,8@@
importmpl_toolkits.mplot3d.axes3dasp3
7
8
7
8
importmatplotlib.animationasanimation
defGen_RandLine(length,dims=2):
10
11
View
9
10
+
+defGen_RandLine(length,dims=2):
11
12
"""
Createalineusingarandomwalkalgorithm
https://github.com/matplotlib/matplotlib/commit/99d3477341f63d802aaf58446c56241fd9869c4f
6/9
11/23/2014
pep8ify:examples/animation99d3477matplotlib/matplotlibGitHub
12
13
@@15,7+16,7@@defGen_RandLine(length,dims=2):
15
16
16
17
"""
lineData=np.empty((dims,length))
17
18
lineData[:,0]=np.random.rand(dims)
19
forindexinrange(1,length):
+forindexinrange(1,length):
20
21
#scalingtherandomnumbersby0.1so
#movementissmallcomparedtoposition.
22
#subtractionby0.5istochangetherangeto[0.5,0.5]
18
19
20
21
@@25,11+26,12@@defGen_RandLine(length,dims=2):
25
26
26
27
27
28
returnlineData
28
defupdate_lines(num,dataLines,lines):
29
forline,datainzip(lines,dataLines):
+
30
31
+defupdate_lines(num,dataLines,lines):
+forline,datainzip(lines,dataLines):
30
32
#NOTE:thereisno.set_data()for3dimdata...
31
32
33
line.set_data(data[0:2,:num])
line.set_3d_properties(data[2,:num])
33
34
35
+line.set_3d_properties(data[2,:num])
returnlines
34
35
36
37
29
#Attaching3Daxistothefigure
@@57,6+59,6@@defupdate_lines(num,dataLines,lines):
57
59
58
59
60
61
#CreatingtheAnimationobject
line_ani=animation.FuncAnimation(fig,update_lines,25,fargs=(data,lines),
62
interval=50,blit=False)
+interval=50,blit=False)
61
63
62
64
plt.show()
60
11 examples/animation/simple_anim.py
@@7,18+7,21@@
8
9
8
9
fig,ax=plt.subplots()
10
10
11
12
11
12
13
x=np.arange(0,2*np.pi,0.01)#xarray
+x=np.arange(0,2*np.pi,0.01)
line,=ax.plot(x,np.sin(x))
14
defanimate(i):
line.set_ydata(np.sin(x+i/10.0))#updatethedata
15
16
+line.set_ydata(np.sin(x+i/10.0))#updatethedata
15
16
17
returnline,
18
#Initonlyrequiredforblittingtogiveacleanslate.
+#Initonlyrequiredforblittingtogiveacleanslate.
19
20
+
+
18
21
definit():
19
22
line.set_ydata(np.ma.array(x,mask=True))
20
21
23
24
returnline,
22
25
ani=animation.FuncAnimation(fig,animate,np.arange(1,200),init_func=init,
26
interval=25,blit=True)
+interval=25,blit=True)
27
plt.show()
13
14
17
23
24
View
6 examples/animation/strip_chart_demo.py
View
@@7,7+7,9@@
importmatplotlib.pyplotasplt
importmatplotlib.animationasanimation
https://github.com/matplotlib/matplotlib/commit/99d3477341f63d802aaf58446c56241fd9869c4f
7/9
11/23/2014
pep8ify:examples/animation99d3477matplotlib/matplotlibGitHub
9
10
10
11
12
classScope:
+
11
12
13
14
def__init__(self,ax,maxt=2,dt=0.02):
self.ax=ax
15
self.dt=dt
13
@@21,7+23,7@@def__init__(self,ax,maxt=2,dt=0.02):
defupdate(self,y):
21
23
22
24
23
24
25
lastt=self.tdata[1]
iflastt>self.tdata[0]+self.maxt:#resetthearrays
26
27
+iflastt>self.tdata[0]+self.maxt:#resetthearrays
25
26
28
self.tdata=[self.tdata[1]]
self.ydata=[self.ydata[1]]
29
self.ax.set_xlim(self.tdata[0],self.tdata[0]+self.maxt)
27
@@48,7+50,7@@defemitter(p=0.03):
48
50
49
50
51
52
#passageneratorin"emitter"toproducedatafortheupdatefunc
ani=animation.FuncAnimation(fig,scope.update,emitter,interval=10,
blit=True)
51
52
53
54
+blit=True)
53
54
55
56
plt.show()
24 examples/animation/subplots.py
@@8,7+8,10@@
8
9
8
9
10
10
11
11
12
13
14
#reallycomplex.Thelengthisduesolelytothefactthatthereareatotal
#of9linesthatneedtobechangedfortheanimationaswellas3subplots
#thatneedinitialsetup.
12
+
+
13
classSubplotAnimation(animation.TimedAnimation):
14
15
16
def__init__(self):
fig=plt.figure()
17
ax1=fig.add_subplot(1,2,1)
@@24,7+27,8@@def__init__(self):
24
27
ax1.set_ylabel('y')
25
28
self.line1=Line2D([],[],color='black')
26
27
29
self.line1a=Line2D([],[],color='red',linewidth=2)
self.line1e=Line2D([],[],color='red',marker='o',markeredgecolor='r')
30
31
+self.line1e=Line2D(
+[],[],color='red',marker='o',markeredgecolor='r')
28
29
32
33
ax1.add_line(self.line1)
30
34
ax1.add_line(self.line1a)
ax1.add_line(self.line1e)
@@36,7+40,8@@def__init__(self):
36
37
40
41
ax2.set_ylabel('z')
self.line2=Line2D([],[],color='black')
38
39
42
self.line2a=Line2D([],[],color='red',linewidth=2)
self.line2e=Line2D([],[],color='red',marker='o',markeredgecolor='r')
43
+self.line2e=Line2D(
40
44
45
+[],[],color='red',marker='o',markeredgecolor='r')
ax2.add_line(self.line2)
41
42
46
47
ax2.add_line(self.line2a)
ax2.add_line(self.line2e)
@@47,7+52,8@@def__init__(self):
ax3.set_ylabel('z')
47
48
52
53
49
54
self.line3=Line2D([],[],color='black')
self.line3a=Line2D([],[],color='red',linewidth=2)
55
self.line3e=Line2D([],[],color='red',marker='o',markeredgecolor='r')
+self.line3e=Line2D(
51
56
57
+[],[],color='red',marker='o',markeredgecolor='r')
ax3.add_line(self.line3)
52
58
ax3.add_line(self.line3a)
53
59
ax3.add_line(self.line3e)
50
View
@@75,19+81,19@@def_draw_frame(self,framedata):
https://github.com/matplotlib/matplotlib/commit/99d3477341f63d802aaf58446c56241fd9869c4f
8/9
11/23/2014
pep8ify:examples/animation99d3477matplotlib/matplotlibGitHub
75
76
81
82
self.line3e.set_data(self.x[head],self.z[head])
77
78
83
self._drawn_artists=[self.line1,self.line1a,self.line1e,
self.line2,self.line2a,self.line2e,
self.line3,self.line3a,self.line3e]
79
84
85
+self.line2,self.line2a,self.line2e,
+self.line3,self.line3a,self.line3e]
80
81
86
87
defnew_frame_seq(self):
82
83
88
89
returniter(range(self.t.size))
84
90
def_init_draw(self):
lines=[self.line1,self.line1a,self.line1e,
self.line2,self.line2a,self.line2e,
85
86
91
self.line3,self.line3a,self.line3e]
+lines=[self.line1,self.line1a,self.line1e,
92
93
+self.line2,self.line2a,self.line2e,
+self.line3,self.line3a,self.line3e]
88
94
forlinlines:
89
90
95
96
l.set_data([],[])
91
92
97
ani=SubplotAnimation()
#ani.save('test_sub.mp4')
98
+#ani.save('test_sub.mp4')
99
plt.show()
87
93
2 examples/animation/unchained.py
View
@@44,7+44,7@@
ha="left",va="bottom",color="w",
44
45
44
45
46
46
family="sansserif",fontweight="bold",fontsize=16)
47
#Updatefunction
+
48
49
defupdate(*args):
#Shiftalldatatotheright
50
data[:,1:]=data[:,:1]
47
48
49
50
Pleasesignintocomment.
https://github.com/matplotlib/matplotlib/commit/99d3477341f63d802aaf58446c56241fd9869c4f
9/9