-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
51 lines (48 loc) · 1.89 KB
/
plot.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
import numpy as np
import matplotlib.pyplot as plt
def draw_relation_lines(size, shaped_points):
for x in range(size):
for y in range(size):
_x, _y = [], []
if y + 1 < size:
_x.append(shaped_points[x, y][0])
_y.append(shaped_points[x, y][1])
_x.append(shaped_points[x, y + 1][0])
_y.append(shaped_points[x, y + 1][1])
if y - 1 >= 0:
_x.append(shaped_points[x, y][0])
_y.append(shaped_points[x, y][1])
_x.append(shaped_points[x, y - 1][0])
_y.append(shaped_points[x, y - 1][1])
if x + 1 < size:
_x.append(shaped_points[x, y][0])
_y.append(shaped_points[x, y][1])
_x.append(shaped_points[x + 1, y][0])
_y.append(shaped_points[x + 1, y][1])
if x - 1 >= 0:
_x.append(shaped_points[x, y][0])
_y.append(shaped_points[x, y][1])
_x.append(shaped_points[x - 1, y][0])
_y.append(shaped_points[x - 1, y][1])
plt.plot(_x, _y, '-r', label="Kohonen")
def plot_figure(reference_points, points, data_n, draw_lines, filename):
# Expect data_n to be a perfect square
size = int(np.sqrt(data_n))
# Reshape to be in matrix form
shape = (size, size, 2)
shaped_points = points.reshape(shape)
# Iterate and plot all connections for each point
plt.clf()
plt.scatter(reference_points[:, 0],
reference_points[:, 1], c='b', marker='s', label="Target")
# Draw the kohonen points
if draw_lines:
draw_relation_lines(size, shaped_points)
else:
_x = shaped_points[:, :, 0]
_y = shaped_points[:, :, 1]
plt.scatter(_x, _y, c='red', label="Kohonen")
plt.axis('off')
plt.axis('equal')
plt.legend()
plt.savefig(filename)