-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizer.py
77 lines (58 loc) · 1.9 KB
/
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
77
import matplotlib.pyplot as plt
from collections import Counter
from pathlib import Path
class CounterGrapher:
def __init__(self, outpath: Path, counter: Counter, n=10, title="", xlabel="", ylabel="", tight_layout=True):
self.outpath = outpath
self.counter = counter
self.title = title
self.xlabel = xlabel
self.ylabel = ylabel
self.tight_layout = tight_layout
if self.counter:
self.labels, self.values = zip(*self.counter.most_common(n))
self.labels = [str(label) for label in self.labels]
self.total = sum(self.values)
self.percentages = [value / self.total for value in self.values]
else:
self.labels, self.values = (), ()
self.total = 0
self.percentages = ()
def bar(self):
plt.clf()
plt.bar(self.labels, self.values)
plt.xlabel(self.xlabel)
plt.ylabel(self.ylabel)
plt.title(self.title)
if self.tight_layout:
plt.tight_layout()
plt.savefig(self.outpath)
plt.close()
return self.outpath
def pie(self):
plt.clf()
plt.pie(self.values, labels=self.labels,
autopct='%1.1f%%', shadow=True, startangle=90)
plt.title(self.title)
if self.tight_layout:
plt.tight_layout()
plt.savefig(self.outpath)
plt.close()
return self.outpath
def hist(self):
plt.clf()
plt.hist(self.values, bins=len(self.counter), align='left')
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.title(self.title)
if self.tight_layout:
plt.tight_layout()
plt.savefig(self.outpath)
plt.close()
return self.outpath
def plot(self):
print(self.bar())
print(self.pie())
print(self.hist())
if __name__ == "__main__":
pass