Skip to content

Commit 16ab277

Browse files
committed
added colors
1 parent a17e2cf commit 16ab277

2 files changed

Lines changed: 13726 additions & 9 deletions

File tree

bin/hist.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@
55
import sys
66

77

8+
bcolours = {
9+
"white": '\033[97m',
10+
"aqua": '\033[96m',
11+
"pink": '\033[95m',
12+
"blue": '\033[94m',
13+
"yellow": '\033[93m',
14+
"green": '\033[92m',
15+
"red": '\033[91m',
16+
"grey": '\033[90m',
17+
"ENDC": '\033[0m'
18+
}
19+
20+
def get_colour(colour):
21+
return bcolours.get(colour, bcolours['white'])
22+
23+
def printcolor(txt, sameline=False, color=get_colour("white")):
24+
if sameline:
25+
print color + txt + bcolours["ENDC"],
26+
else:
27+
print color + txt + bcolours["ENDC"]
28+
829
def drange(start, stop, step=1.0):
930
"generate between 2 numbers w/ optional step"
1031
r = start
@@ -28,11 +49,14 @@ def read_numbers(numbers):
2849
for n in open(numbers):
2950
yield float(n.strip())
3051

31-
def plot_hist(f, height=20, bincount=None, pch="o"):
52+
def plot_hist(f, height=20, bincount=None, pch="o", colour="white"):
3253
"plot a histogram given a file of numbers"
3354
#first apss
3455
if pch is None:
3556
pch = "o"
57+
58+
colour = get_colour(colour)
59+
3660
min_val, max_val = None, None
3761
n = 0.
3862
for number in read_numbers(f):
@@ -66,26 +90,26 @@ def plot_hist(f, height=20, bincount=None, pch="o"):
6690
ylab = str(y)
6791
ylab += " "*(nlen - len(ylab)) + "|"
6892

69-
print ylab,
93+
printcolor(ylab, True, colour)
7094

7195
for i in range(len(hist)):
7296
if y < hist[i]:
73-
print pch,
97+
printcolor(pch, True, colour)
7498
else:
75-
print " ",
99+
printcolor(" ", True, colour)
76100
print
77101
xs = hist.keys() * 2
78102

79-
print " "*(nlen+1) + "-"*len(xs)
103+
printcolor(" "*(nlen+1) + "-"*len(xs), False, colour)
80104

81105
for i in range(0, nlen):
82-
print " "*(nlen+1),
106+
printcolor(" "*(nlen+1), True, colour)
83107
for x in range(0, len(hist)):
84108
num = str(bins[x])
85109
if x%2==0:
86-
print " ",
110+
printcolor(" ", True, colour)
87111
elif i < len(num):
88-
print num[i],
112+
printcolor(num[i], True, colour)
89113
print
90114

91115
summary = "Summary\n--------\nMax: %s\nMin: %s\nCount: %s" % (min_val, max_val, int(n))
@@ -102,10 +126,12 @@ def plot_hist(f, height=20, bincount=None, pch="o"):
102126
parser.add_option('-s', '--height', help='height of the histogram (in lines)',
103127
default=20, dest='h')
104128
parser.add_option('-p', '--pch', help='shape of each bar', default='o', dest='p')
129+
parser.add_option('-c', '--colour', help='colour of the plot', default='white', dest='colour')
130+
105131
(opts, args) = parser.parse_args()
106132

107133
if opts.f is None:
108134
opts.f = args[0]
109135

110-
plot_hist(opts.f, opts.h, opts.b, opts.p)
136+
plot_hist(opts.f, opts.h, opts.b, opts.p, opts.colour)
111137

0 commit comments

Comments
 (0)