Skip to content

Commit 1cf7ddc

Browse files
committed
Fixed colour printing to work on white backgrounds
1 parent 00dc580 commit 1cf7ddc

File tree

3 files changed

+93
-85
lines changed

3 files changed

+93
-85
lines changed

‎bashplotlib/histogram.py‎

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
23

4+
"""
5+
Plotting terminal based histograms
6+
"""
7+
8+
importos
9+
importsys
310
importmath
411
importoptparse
5-
importos
612
fromos.pathimportdirname
7-
importsys
8-
fromutils.helpersimport*
9-
fromutils.commandhelpimporthist
13+
from .utils.helpersimport*
14+
from.utils.commandhelpimporthist
15+
1016

1117
defcalc_bins(n, min_val, max_val, h=None, binwidth=None):
12-
"calculate number of bins for the histogram"
18+
"""
19+
Calculate number of bins for the histogram
20+
"""
1321
ifnoth:
1422
h=max(10, math.log(n+1, 2))
1523
ifbinwidth==0:
@@ -22,15 +30,17 @@ def calc_bins(n, min_val, max_val, h=None, binwidth=None):
2230
else:
2331
yieldb
2432

33+
2534
defread_numbers(numbers):
26-
"read the input data in the most optimal way"
35+
"""
36+
Read the input data in the most optimal way
37+
"""
2738
ifisinstance(numbers, list):
28-
forninnumbers:
29-
n=str(n)
30-
yieldfloat(n.strip())
39+
fornumberinnumbers:
40+
yieldfloat(str(number).strip())
3141
else:
32-
forninopen(numbers):
33-
yieldfloat(n.strip())
42+
fornumberinopen(numbers):
43+
yieldfloat(number.strip())
3444

3545

3646
defrun_demo():
@@ -53,8 +63,8 @@ def run_demo():
5363
plot_hist(demo_file)
5464
print"*"*80
5565

56-
#with colors
57-
print"histogram with colors"
66+
#with colours
67+
print"histogram with colours"
5868
print"plot_hist('%s', colour='blue')"%demo_file
5969
print"hist -f %s -c blue"%demo_file
6070
plot_hist(demo_file, colour='blue')
@@ -74,10 +84,11 @@ def run_demo():
7484
plot_hist(demo_file, height=35.0, bincount=40)
7585

7686

77-
defplot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="white", title="", xlab=None, showSummary=False, regular=False):
78-
"""make a histogram
87+
defplot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="default", title="", xlab=None, showSummary=False, regular=False):
88+
"""
89+
Make a histogram
7990
80-
Keyword arguments:
91+
Arguments:
8192
height -- the height of the histogram in # of lines
8293
bincount -- number of bins in the histogram
8394
binwidth -- width of bins in the histogram
@@ -88,28 +99,25 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="whi
8899
showSummary -- boolean value for whether or not to display a summary
89100
regular -- boolean value for whether or not to start y-labels at 0
90101
"""
91-
92102
ifpchisNone:
93103
pch="o"
94104

95-
colour=get_colour(colour)
96-
97105
min_val, max_val=None, None
98-
n, mean=0., 0.
106+
n, mean=0.0, 0.0
107+
99108
fornumberinread_numbers(f):
100109
n+=1
101-
102-
if (min_valisNone) or (number<min_val):
110+
ifmin_valisNoneornumber<min_val:
103111
min_val=number
104-
if(max_valisNone)or(number>max_val):
112+
ifmax_valisNoneornumber>max_val:
105113
max_val=number
106114
mean+=number
115+
107116
mean/=n
108117

109118
bins=list(calc_bins(n, min_val, max_val, bincount, binwidth))
110-
hist={}
111-
foriinrange(len(bins)):
112-
hist[i] =0
119+
hist={i: 0foriinrange(len(bins))}
120+
113121
fornumberinread_numbers(f):
114122
fori, binenumerate(bins):
115123
ifnumber<=b:
@@ -122,6 +130,7 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="whi
122130

123131
start=max(min_y, 1)
124132
stop=max_y+1
133+
125134
ifregular:
126135
start=1
127136

@@ -138,6 +147,7 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="whi
138147
iftitle:
139148
printbox_text(title, max(len(hist)*2, len(title)), nlen)
140149
print
150+
141151
used_labs=set()
142152
foryinys:
143153
ylab=str(int(y))
@@ -151,67 +161,63 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="whi
151161

152162
foriinrange(len(hist)):
153163
ifint(y) <=hist[i]:
154-
printcolor(pch, True, colour)
164+
printcolour(pch, True, colour)
155165
else:
156-
printcolor(" ", True, colour)
166+
printcolour(" ", True, colour)
157167
print
158168
xs=hist.keys() *2
159169

160-
print" "*(nlen+1) +"-"*len(xs)
161-
170+
print" "* (nlen+1) +"-"*len(xs)
162171

163172
ifxlab:
164173
xlen=len(str(float((max_y)/height) +max_y))
165174
foriinrange(0, xlen):
166-
printcolor(" "*(nlen+1), True, colour)
175+
printcolour(" "*(nlen+1), True, colour)
167176
forxinrange(0, len(hist)):
168177
num=str(bins[x])
169-
ifx%2==0:
178+
ifx%2==0:
170179
print" ",
171180
elifi<len(num):
172181
printnum[i],
173182
else:
174183
print" ",
175184
print
185+
176186
center=max(map(len, map(str, [n, min_val, mean, max_val])))
177187
center+=15
178188

179189
ifshowSummary:
180190
print
181-
print"-"*(2+center)
191+
print"-"*(2+center)
182192
print"|"+"Summary".center(center) +"|"
183-
print"-"*(2+center)
193+
print"-"*(2+center)
184194
summary="|"+ ("observations: %d"%n).center(center) +"|\n"
185195
summary+="|"+ ("min value: %f"%min_val).center(center) +"|\n"
186196
summary+="|"+ ("mean : %f"%mean).center(center) +"|\n"
187197
summary+="|"+ ("max value: %f"%max_val).center(center) +"|\n"
188-
summary+="-"*(2+center)
198+
summary+="-"*(2+center)
189199
printsummary
190200

191201

192202
defmain():
193203

194204
parser=optparse.OptionParser(usage=hist['usage'])
195205

196-
parser.add_option('-f', '--file', help='a file containing a column of numbers',
197-
default=None, dest='f')
198-
parser.add_option('-t', '--title', help='title for the chart',
199-
default="", dest='t')
200-
parser.add_option('-b', '--bins', help='number of bins in the histogram',
201-
type='int', default=None, dest='b')
202-
parser.add_option('-w', '--binwidth', help='width of bins in the histogram',
203-
type='float', default=None, dest='binwidth')
204-
parser.add_option('-s', '--height', help='height of the histogram (in lines)',
205-
type='int', default=None, dest='h')
206+
parser.add_option('-f', '--file', help='a file containing a column of numbers', default=None, dest='f')
207+
parser.add_option('-t', '--title', help='title for the chart', default="", dest='t')
208+
parser.add_option('-b', '--bins', help='number of bins in the histogram', type='int', default=None, dest='b')
209+
parser.add_option('-w', '--binwidth', help='width of bins in the histogram', type='float', default=None, dest='binwidth')
210+
parser.add_option('-s', '--height', help='height of the histogram (in lines)', type='int', default=None, dest='h')
206211
parser.add_option('-p', '--pch', help='shape of each bar', default='o', dest='p')
207212
parser.add_option('-x', '--xlab', help='label bins on x-axis', default=None, action="store_true", dest='x')
208-
parser.add_option('-c', '--colour', help='colour of the plot (%s)'%", ".join([cforcinbcolours.keys() ifc!='ENDC']),
209-
default='white', dest='colour')
213+
parser.add_option('-c', '--colour', help='colour of the plot (%s)'%colour_help, default='default', dest='colour')
210214
parser.add_option('-d', '--demo', help='run demos', action='store_true', dest='demo')
211215
parser.add_option('-n', '--nosummary', help='hide summary', action='store_false', dest='showSummary', default=True)
212-
parser.add_option('-r', '--regular', help='use regular y-scale (0 - maximum y value), instead of truncated y-scale (minimum y-value - maximum y-value)', default=False, action="store_true", dest='regular')
216+
parser.add_option('-r', '--regular',
217+
help='use regular y-scale (0 - maximum y value), instead of truncated y-scale (minimum y-value - maximum y-value)',
218+
default=False, action="store_true", dest='regular')
213219

214-
(opts, args)=parser.parse_args()
220+
opts, args=parser.parse_args()
215221

216222
ifopts.fisNone:
217223
iflen(args) >0:
@@ -227,5 +233,5 @@ def main():
227233
print"nothing to plot!"
228234

229235

230-
if__name__=="__main__":
236+
if__name__=="__main__":
231237
main()

‎bashplotlib/scatterplot.py‎

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Plotting terminal based scatterplots
6+
"""
27

38
importcsv
4-
importoptparse
59
importsys
6-
fromutils.helpersimport*
7-
fromutils.commandhelpimportscatter
10+
importoptparse
11+
from .utils.helpersimport*
12+
from .utils.commandhelpimportscatter
813

914

1015
defget_scale(series, is_y=False, steps=20):
@@ -20,10 +25,12 @@ def get_scale(series, is_y=False, steps=20):
2025
scaled_series.reverse()
2126
returnscaled_series
2227

28+
2329
defplot_scatter(f, xs, ys, size, pch, colour, title):
24-
"""Form a complex number.
30+
"""
31+
Form a complex number.
2532
26-
Arguments:
33+
Arguments:
2734
f -- comma delimited file w/ x,y coordinates
2835
xs -- if f not specified this is a file w/ x coordinates
2936
ys -- if f not specified this is a filew / y coordinates
@@ -44,14 +51,12 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
4451
xs= [float(str(row).strip()) forrowinopen(xs)]
4552
ys= [float(str(row).strip()) forrowinopen(ys)]
4653

47-
colour=get_colour(colour)
48-
4954
plotted=set()
5055

5156
iftitle:
5257
printbox_text(title, 2*len(get_scale(xs, False, size))+1)
5358

54-
print"-"*(2*len(get_scale(xs, False, size))+2)
59+
print"-"*(2*len(get_scale(xs, False, size))+2)
5560
foryinget_scale(ys, True, size):
5661
print"|",
5762
forxinget_scale(xs, False, size):
@@ -67,30 +72,24 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
6772
point="|"
6873
elify==0:
6974
point="-"
70-
printcolor(point, True, colour)
75+
printcolour(point, True, colour)
7176
print"|"
7277
print"-"*(2*len(get_scale(xs, False, size))+2)
7378

7479

7580
defmain():
7681

7782
parser=optparse.OptionParser(usage=scatter['usage'])
78-
parser.add_option('-f', '--file', help='a csv w/ x and y coordinates',
79-
default=None, dest='f')
80-
parser.add_option('-t', '--title', help='title for the chart',
81-
default="", dest='t')
82-
parser.add_option('-x', help='x coordinates',
83-
default=None, dest='x')
84-
parser.add_option('-y', help='y coordinates',
85-
default=None, dest='y')
86-
parser.add_option('-s', '--size',help='y coordinates',
87-
default=20, dest='size', type='int')
88-
parser.add_option('-p', '--pch',help='shape of point',
89-
default="x", dest='pch')
90-
parser.add_option('-c', '--colour', help='colour of the plot (%s)'%", ".join(bcolours.keys()),
91-
default='white', dest='colour')
92-
93-
(opts, args) =parser.parse_args()
83+
84+
parser.add_option('-f', '--file', help='a csv w/ x and y coordinates', default=None, dest='f')
85+
parser.add_option('-t', '--title', help='title for the chart', default="", dest='t')
86+
parser.add_option('-x', help='x coordinates', default=None, dest='x')
87+
parser.add_option('-y', help='y coordinates', default=None, dest='y')
88+
parser.add_option('-s', '--size',help='y coordinates', default=20, dest='size', type='int')
89+
parser.add_option('-p', '--pch',help='shape of point', default="x", dest='pch')
90+
parser.add_option('-c', '--colour', help='colour of the plot (%s)'%colour_help, default='default', dest='colour')
91+
92+
opts, args=parser.parse_args()
9493

9594
ifopts.fisNoneand (opts.xisNoneoropts.yisNone):
9695
opts.f=sys.stdin.readlines()

‎bashplotlib/utils/helpers.py‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88
importsys
99

1010
bcolours={
11-
"white": '\033[97m',
12-
"aqua": '\033[96m',
13-
"pink": '\033[95m',
14-
"blue": '\033[94m',
15-
"yellow": '\033[93m',
16-
"green": '\033[92m',
17-
"red": '\033[91m',
18-
"grey": '\033[90m',
19-
"black": '\033[30m',
20-
"ENDC": '\033[39m',
11+
"white": '\033[97m',
12+
"aqua": '\033[96m',
13+
"pink": '\033[95m',
14+
"blue": '\033[94m',
15+
"yellow": '\033[93m',
16+
"green": '\033[92m',
17+
"red": '\033[91m',
18+
"grey": '\033[90m',
19+
"black": '\033[30m',
20+
"default": '\033[39m',
21+
"ENDC": '\033[39m',
2122
}
2223

24+
colour_help=', '.join([colourforcolourinbcoloursifcolour!="ENDC"])
25+
2326

2427
defget_colour(colour):
2528
"""

0 commit comments

Comments
(0)