-
Notifications
You must be signed in to change notification settings - Fork 0
/
pause_real_time
55 lines (39 loc) · 1.33 KB
/
pause_real_time
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
# draw each point one by one
# import numpy as np
# import matplotlib.pyplot as plt
# plt.axis([0, 10, 0, 1])
# for i in range(10):
# y = np.random.random()
# plt.scatter(i, y)
# plt.pause(0.05)
# plt.show()
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
class PauseAnimation:
def __init__(self):
fig, ax = plt.subplots()
ax.set_title('Press any key to pause/resume the animation')
x = np.linspace(-0.1, 0.1, 1000)
# Start with a normal distribution
self.n0 = (1.0 / ((4 * np.pi * 2e-4 * 0.1) ** 0.5)
* np.exp(-x ** 2 / (4 * 2e-4 * 0.1)))
self.p, = ax.plot(x, self.n0)
self.animation = animation.FuncAnimation(
fig, self.update, frames=200, interval=50, blit=True)
self.paused = False
# 'key_press_event' for keyboard
# 'button_press_event' for mouse button
fig.canvas.mpl_connect('key_press_event', self.toggle_pause)
def toggle_pause(self, *args, **kwargs):
if self.paused:
self.animation.resume()
else:
self.animation.pause()
self.paused = not self.paused
def update(self, i):
self.n0 += i / 100 % 5
self.p.set_ydata(self.n0 % 20)
return (self.p,)
pa = PauseAnimation()
plt.show()