forked from tteague6/PHYS390Clustering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
55 lines (41 loc) · 1.52 KB
/
utilities.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
# Handy functions that are not necessary for the conceptual understanding of
# this code example
from collections import defaultdict
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
def get_groupings(sample):
"""Grab selected items and group them by color"""
colored_points = defaultdict(list)
for label, bool_val in sample.items():
# Skip over items that were not selected
if not bool_val:
continue
# Parse selected items
# Note: label look like "<x_coord>,<y_coord>_<color>"
coord, color = label.split("_")
coord_tuple = tuple(map(float, coord.split(",")))
colored_points[color].append(coord_tuple)
return dict(colored_points)
def visualize_groupings(groupings_dict, filename):
"""
Args:
groupings_dict: key is a color, value is a list of x-y coordinate tuples.
For example, {'r': [(0,1), (2,3)], 'b': [(8,3)]}
filename: name of the file to save plot in
"""
for color, points in groupings_dict.items():
# Ignore items that do not contain any coordinates
if not points:
continue
# Populate plot
point_style = color + "o"
plt.plot(*zip(*points), point_style)
plt.savefig(filename)
def visualize_scatterplot(x_y_tuples_list, filename):
"""Plotting out a list of x-y tuples
Args:
x_y_tuples_list: A list of x-y coordinate values. e.g. [(1,4), (3, 2)]
"""
plt.plot(*zip(*x_y_tuples_list), "o")
plt.savefig(filename)