Skip to content

Commit 0f25a51

Browse files
author
Jonathan Rocher
committed
Adding other ways to do animations in MPL.
1 parent dd2edf0 commit 0f25a51

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

matplotlib/animation_demo4.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import matplotlib.animation as animation
4+
5+
def update_line(num, data, line):
6+
line.set_data(data[...,:num])
7+
return line,
8+
9+
fig1 = plt.figure()
10+
11+
data = np.random.rand(2, 25)
12+
l, = plt.plot([], [], 'r-')
13+
plt.xlim(0, 1)
14+
plt.ylim(0, 1)
15+
plt.xlabel('x')
16+
plt.title('test')
17+
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
18+
interval=50, blit=True)
19+
#line_ani.save('lines.mp4')
20+
21+
fig2 = plt.figure()
22+
23+
x = np.arange(-9, 10)
24+
y = np.arange(-9, 10).reshape(-1, 1)
25+
base = np.hypot(x, y)
26+
ims = []
27+
for add in np.arange(15):
28+
ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))
29+
30+
im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
31+
blit=True)
32+
#im_ani.save('im.mp4', metadata={'artist':'Guido'})
33+
34+
plt.show()

matplotlib/animation_demo5.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import matplotlib.pyplot as plt
2+
import matplotlib.animation as animation
3+
import numpy as np
4+
from mpl_toolkits.mplot3d import Axes3D
5+
6+
FLOOR = -10
7+
CEILING = 10
8+
9+
class AnimatedScatter(object):
10+
def __init__(self, numpoints=5):
11+
self.numpoints = numpoints
12+
self.stream = self.data_stream()
13+
self.angle = 0
14+
15+
self.fig = plt.figure()
16+
self.fig.canvas.mpl_connect('draw_event',self.forceUpdate)
17+
self.ax = self.fig.add_subplot(111,projection = '3d')
18+
self.ani = animation.FuncAnimation(self.fig, self.update, interval=100,
19+
init_func=self.setup_plot, frames=20)
20+
21+
def change_angle(self):
22+
self.angle = (self.angle + 1)%360
23+
24+
def forceUpdate(self, event):
25+
self.scat.changed()
26+
27+
def setup_plot(self):
28+
X = next(self.stream)
29+
c = ['b', 'r', 'g', 'y', 'm']
30+
self.scat = self.ax.scatter(X[:,0], X[:,1], X[:,2] , c=c, s=200)
31+
32+
self.ax.set_xlim3d(FLOOR, CEILING)
33+
self.ax.set_ylim3d(FLOOR, CEILING)
34+
self.ax.set_zlim3d(FLOOR, CEILING)
35+
36+
return self.scat,
37+
38+
def data_stream(self):
39+
data = np.zeros(( self.numpoints , 3 ))
40+
xyz = data[:,:3]
41+
while True:
42+
xyz += 2 * (np.random.random(( self.numpoints,3)) - 0.5)
43+
yield data
44+
45+
def update(self, i):
46+
data = next(self.stream)
47+
self.scat._offsets3d = ( np.ma.ravel(data[:,0]) , np.ma.ravel(data[:,1]) , np.ma.ravel(data[:,2]) )
48+
return self.scat,
49+
50+
def show(self):
51+
plt.show()
52+
53+
if __name__ == '__main__':
54+
a = AnimatedScatter()
55+
# Saving to a movie file requires for ffmpeg or mencoder or something like
56+
# that to be present on the machine/passed to the save function.
57+
#a.ani.save("movie.avi", codec='avi')
58+
a.show()

0 commit comments

Comments
 (0)