-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimationtest.py
More file actions
82 lines (66 loc) · 2.08 KB
/
animationtest.py
File metadata and controls
82 lines (66 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
=========================
Simple animation examples
=========================
This example contains two animations. The first is a random walk plot. The
second is an image animation.
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# fig2 = plt.figure()
# x = np.arange(-9, 10)
# y = np.arange(-9, 10).reshape(-1, 1)
# base = np.hypot(x, y)
# ims = []
# for add in np.arange(15):
# ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))
# print(ims[0])
# im_ani = animation.ArtistAnimation(fig2, ims, interval=500, repeat_delay=3000,
# blit=True)
def data_gen():
t = data_gen.t
cnt = 0
while cnt < 1000:
cnt+=1
t += 0.05
y1 = np.sin(2*np.pi*t) * np.exp(-t/10.)
y2 = np.cos(2*np.pi*t) * np.exp(-t/10.)
# adapted the data generator to yield both sin and cos
yield t, y1, y2
data_gen.t = 0
# create a figure with two subplots
fig, (ax1, ax2) = plt.subplots(2,1)
# intialize two line objects (one in each axes)
line1, = ax1.plot([], [], lw=2)
line2, = ax2.plot([], [], lw=2, color='r')
line = [line1, line2]
# the same axes initalizations as before (just now we do it for both of them)
for ax in [ax1, ax2]:
ax.set_ylim(-1.1, 1.1)
ax.set_xlim(0, 5)
ax.grid()
# initialize the data arrays
xdata, y1data, y2data = [], [], []
def run(data):
# update the data
t, y1, y2 = data
xdata.append(t)
y1data.append(y1)
y2data.append(y2)
# axis limits checking. Same as before, just for both axes
for ax in [ax1, ax2]:
xmin, xmax = ax.get_xlim()
if t >= xmax:
ax.set_xlim(xmin, 2*xmax)
ax.figure.canvas.draw()
# update the data of both line objects
line[0].set_data(xdata, y1data)
line[1].set_data(xdata, y2data)
return line
ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10,
repeat=False)
plt.show()
# To save this second animation with some metadata, use the following command:
# im_ani.save('im.mp4', metadata={'artist':'Guido'})
# plt.show()