-
Notifications
You must be signed in to change notification settings - Fork 0
/
animated_visualizer.py
76 lines (55 loc) · 2.24 KB
/
animated_visualizer.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
74
75
76
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
import csv
names=[]
xs = []
ys = []
with open('Locations.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
# print(f'\t{row[0]} ***** {row[1]} ***** {row[2]}.')
names.append(row[0])
xs.append(float(row[2]))
ys.append(float(row[1]))
def animateTSP(history, points):
print(points)
print(history)
key_frames_mult = len(history) // 1500
fig, ax = plt.subplots()
for i, txt in enumerate(names):
ax.annotate(txt, (xs[i],ys[i]))
time_text = fig.text(0.02, 0.95, '')
''' path is a line coming through all the nodes '''
line, = plt.plot([], [], lw=2)
def init():
''' initialize node dots on graph '''
x = [points[i][0] for i in history[0]]
y = [points[i][1] for i in history[0]]
time_text.set_text('Start')
plt.plot(x, y, 'co')
''' draw axes slighty bigger '''
extra_x = (max(x) - min(x)) * 0.05
extra_y = (max(y) - min(y)) * 0.05
ax.set_xlim(min(x) - extra_x, max(x) + extra_x)
ax.set_ylim(min(y) - extra_y, max(y) + extra_y)
'''initialize solution to be empty '''
line.set_data([], [])
return line, time_text
def update(frame):
''' for every frame update the solution on the graph '''
x = [points[i, 0] for i in history[frame] + [history[frame][0]]]
y = [points[i, 1] for i in history[frame] + [history[frame][0]]]
ax.set_title('Frame ' + str(frame))
line.set_data(x, y)
# time_text.set_text(str(history[frame]))
return line ,time_text
''' animate precalulated solutions '''
ani = FuncAnimation(fig, update, frames=range(0, len(history), key_frames_mult), # stuur x points en y points aan en werk die distance uit
init_func=init, interval=3, repeat=False)
plt.show()