-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualize.py
57 lines (39 loc) · 1.23 KB
/
visualize.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
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# 读取模拟文件
def read_simulate_res():
res = []
with open('simulate-res.txt') as dataFile:
content = dataFile.read()
dataLines = content.split('\n')
dataLinesIter = iter(dataLines)
# 读到 [BEGIN] 位置
while next(dataLinesIter) != '[BEGIN]':
pass
for data in dataLinesIter:
if data == '[END]':
break
bodyLocs = data.strip().split(' ')
resX, resY = [], []
for i, elem in enumerate(bodyLocs):
if elem == '-':
continue
if i % 2 == 0:
resX.append(float(elem))
else:
resY.append(float(elem))
res.append((resX, resY))
return res
# 初始化 matplotlib
fig, ax = plt.subplots()
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
line, = ax.plot([], [], '.b')
frameData = read_simulate_res()
def ani_func(i):
line.set_data(frameData[i][0], frameData[i][1])
return line,
def ani_init():
return ani_func(0)
anim = FuncAnimation(fig, ani_func, init_func=ani_init, frames=len(frameData), interval=1)
plt.show()