-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.py
73 lines (48 loc) · 1.55 KB
/
animation.py
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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def f(a):
return a +np.random.random()
class AnimaPlot(object):
def __init__(self,ax,data_lengh=200):
self.data = 0
self.data_lengh = data_lengh
self.line, = ax.plot([], [], '.-r')
self.x = []
self.y = []
self.ax = ax
self.ax.set_xlim(0, 21)
self.ax.set_ylim(0, 21)
self.ax.grid(True)
# self.ax.axvline(prob, linestyle='--', color='black')
def init(self):
self.ax.axvline(self.data, linestyle=':', color='black')
self.line.set_data([], [])
return self.line,
def __call__(self, i):
if i == 0:
return self.init()
if len(self.x) >self.data_lengh:
del(self.x[0])
if len(self.y)> self.data_lengh:
del(self.y[0])
self.x.append(self.data)
self.x_=self.data
self.y_=self.data
self.data = f(self.data)
self.y.append(f(self.data))
self.line.set_data(self.x, self.y)
self.ax.set_xlim(self.data-50, self.data)
self.ax.set_ylim(self.data-50, self.data)
return self.line,
#frames
fig, ax = plt.subplots()
ap = AnimaPlot(ax)
_ = FuncAnimation(fig, ap, frames=100, init_func=ap.init,
interval=100, blit=False)
plt.show()
#frames 帧 设定帧长度
#blit 动态移动坐标。为True时坐标固定
#interval 更新间隔:单位毫秒
#self.x和self.y设置成数值,为坐标的点
#