-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththread_plot.py
More file actions
47 lines (38 loc) · 1.37 KB
/
thread_plot.py
File metadata and controls
47 lines (38 loc) · 1.37 KB
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
from collections import defaultdict
from matplotlib.patches import Rectangle
class TreeNode:
def __init__(self):
self.count = 0
self.children = defaultdict(TreeNode)
def add(self, s):
self.count += 1
if s:
k, rest = s[0], s[1:]
self.children[k].add(rest)
def __str__(self):
retval = [f'{self.count}']
for char in sorted(self.children):
retval.append(f'{char} {self.children[t]}')
return '\n'.join(retval)
def walk(self, index=0):
for char in sorted(self.children):
yield index, char, self.children[char].count
for result in self.children[char].walk(index + 1):
yield result
def get_patches(self, index=0, base=0):
offset = 0
for char, child in sorted(self.children.items()):
yield (index, base + offset, 1, child.count, char)
for patch in child.get_patches(index+1, base + offset):
yield patch
offset += child.count
def thread_plot(ax, colormap, data):
root = TreeNode()
max_len = 0
for s in data:
root.add(s)
max_len = max(max_len, len(s))
for (x, y, w, h, k) in root.get_patches():
ax.add_patch(Rectangle((x, y), w, h, color=colormap[k]))
ax.set_xlim(0, max_len)
ax.set_ylim(0, len(data))