Skip to content

Commit a14c5d2

Browse files
committed
added colour to scatterplot
1 parent 16ab277 commit a14c5d2

File tree

6 files changed

+44
-44
lines changed

6 files changed

+44
-44
lines changed

bin/hist.py

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,9 @@
33
from collections import Counter
44
import optparse
55
import sys
6+
from utils.helpers import *
67

78

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-
29-
def drange(start, stop, step=1.0):
30-
"generate between 2 numbers w/ optional step"
31-
r = start
32-
while r < stop:
33-
yield r
34-
r += step
35-
369
def calc_bins(n, min_val, max_val, h=None):
3710
"calculate number of bins for the histogram"
3811
if not h:
@@ -90,7 +63,7 @@ def plot_hist(f, height=20, bincount=None, pch="o", colour="white"):
9063
ylab = str(y)
9164
ylab += " "*(nlen - len(ylab)) + "|"
9265

93-
printcolor(ylab, True, colour)
66+
print ylab,
9467

9568
for i in range(len(hist)):
9669
if y < hist[i]:
@@ -100,16 +73,16 @@ def plot_hist(f, height=20, bincount=None, pch="o", colour="white"):
10073
print
10174
xs = hist.keys() * 2
10275

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

10578
for i in range(0, nlen):
10679
printcolor(" "*(nlen+1), True, colour)
10780
for x in range(0, len(hist)):
10881
num = str(bins[x])
10982
if x%2==0:
110-
printcolor(" ", True, colour)
83+
print " ",
11184
elif i < len(num):
112-
printcolor(num[i], True, colour)
85+
print num[i],
11386
print
11487

11588
summary = "Summary\n--------\nMax: %s\nMin: %s\nCount: %s" % (min_val, max_val, int(n))
@@ -126,7 +99,8 @@ def plot_hist(f, height=20, bincount=None, pch="o", colour="white"):
12699
parser.add_option('-s', '--height', help='height of the histogram (in lines)',
127100
default=20, dest='h')
128101
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')
102+
parser.add_option('-c', '--colour', help='colour of the plot (%s)' % ", ".join(bcolours.keys()),
103+
default='white', dest='colour')
130104

131105
(opts, args) = parser.parse_args()
132106

bin/scatter.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
#!/usr/bin/python
22
import csv
33
import optparse
4+
from utils.helpers import *
45

56

6-
def drange(start, stop, step=1.0):
7-
"generate between 2 numbers w/ optional step"
8-
r = start
9-
while r < stop:
10-
yield r
11-
r += step
12-
137
def get_scale(series, is_y=False, steps=20):
148
min_val = min(series)
159
max_val = max(series)
@@ -23,7 +17,7 @@ def get_scale(series, is_y=False, steps=20):
2317
scaled_series.reverse()
2418
return scaled_series
2519

26-
def plot_scatter(f, xs, ys, size, pch):
20+
def plot_scatter(f, xs, ys, size, pch, colour):
2721
if f:
2822
data = [tuple(map(float, line)) for line in csv.reader(open(f))]
2923
xs = [i[0] for i in data]
@@ -32,6 +26,8 @@ def plot_scatter(f, xs, ys, size, pch):
3226
xs = [float(row.strip()) for row in open(xs)]
3327
ys = [float(row.strip()) for row in open(ys)]
3428

29+
colour = get_colour(colour)
30+
3531
plotted = set()
3632

3733
print "-"*(2*len(get_scale(xs, False, size))+2)
@@ -50,7 +46,7 @@ def plot_scatter(f, xs, ys, size, pch):
5046
point = "|"
5147
elif y==0:
5248
point = "-"
53-
print point,
49+
printcolor(point, True, colour)
5450
print "|"
5551
print "-"*(2*len(get_scale(xs, False, size))+2)
5652

@@ -68,9 +64,10 @@ def plot_scatter(f, xs, ys, size, pch):
6864
default=20, dest='size', type='int')
6965
parser.add_option('-p', '--pch',help='shape of point',
7066
default="x", dest='pch')
71-
67+
parser.add_option('-c', '--colour', help='colour of the plot (%s)' % ", ".join(bcolours.keys()),
68+
default='white', dest='colour')
7269

7370
(opts, args) = parser.parse_args()
7471

75-
plot_scatter(opts.f, opts.x, opts.y, opts.size, opts.pch)
72+
plot_scatter(opts.f, opts.x, opts.y, opts.size, opts.pch, opts.colour)
7673

bin/utils/__init__.py

Whitespace-only changes.

bin/utils/__init__.pyc

144 Bytes
Binary file not shown.

bin/utils/helpers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
bcolours = {
4+
"white": '\033[97m',
5+
"aqua": '\033[96m',
6+
"pink": '\033[95m',
7+
"blue": '\033[94m',
8+
"yellow": '\033[93m',
9+
"green": '\033[92m',
10+
"red": '\033[91m',
11+
"grey": '\033[90m',
12+
"ENDC": '\033[0m'
13+
}
14+
15+
def get_colour(colour):
16+
return bcolours.get(colour, bcolours['white'])
17+
18+
def printcolor(txt, sameline=False, color=get_colour("white")):
19+
if sameline:
20+
print color + txt + bcolours["ENDC"],
21+
else:
22+
print color + txt + bcolours["ENDC"]
23+
24+
def drange(start, stop, step=1.0):
25+
"generate between 2 numbers w/ optional step"
26+
r = start
27+
while r < stop:
28+
yield r
29+
r += step

bin/utils/helpers.pyc

1.14 KB
Binary file not shown.

0 commit comments

Comments
 (0)