-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarplots.py
39 lines (36 loc) · 1.32 KB
/
barplots.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
from itertools import zip_longest
_justs = {'left': str.ljust, 'right': str.rjust, 'center': str.center}
def barplot(data, labels=None, char='x', colons=True, border=' ', just='left',
pad=0):
just = _justs[just]
if labels:
labels = list(map(str, labels))
if colons:
labels = [l + ':' if l else '' for l in labels]
lw = max(map(len, labels))
sep = (' '*lw + border + '\n')*pad
past_first = False
for l, x in zip_longest(labels, data, fillvalue=0):
print(sep*past_first + just(l or '', lw) + border + char*x)
past_first = True
else:
for x in data:
print(char * x + '\n'*pad)
def vbarplot(data, labels=None, flipped=False, char='x', border='',
just='center', pad=1):
just = _justs[just]
w = 1
if labels:
labels = list(map(str, labels))
w = max(map(len, labels))
header = (' '*pad).join(just(l, w) for l in labels)
if not flipped:
print(header)
if border:
print(border*len(header))
for i in range(max(data, default=0))[::-1 if flipped else 1]:
print((' '*pad).join(just(char if x > i else ' ', w) for x in data))
if labels and flipped:
if border:
print(border*len(header))
print(header)