Skip to content

Commit d7fd699

Browse files
author
TrungNT
committed
Stable, add bar plot
1 parent 80d6c7a commit d7fd699

File tree

4 files changed

+74
-49
lines changed

4 files changed

+74
-49
lines changed

bashplotlib/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .histogram import plot_hist
2+
from .scatterplot import plot_scatter
3+
from .barplot import plot_bar

bashplotlib/histogram.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def run_demo():
8888

8989
def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o",
9090
colour="default", title="", xlab=None, showSummary=False,
91-
regular=False, print_func=print):
91+
regular=False, return_str=False):
9292
''' Plot histogram.
9393
1801| oo
9494
1681| oo
@@ -123,12 +123,12 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o",
123123
whether or not to display a summary
124124
regular : boolean
125125
whether or not to start y-labels at 0
126-
print_func : callable
127-
custom print function
126+
return_str : boolean
127+
return string represent the plot or print it out, default: False
128128
'''
129129
if pch is None:
130130
pch = "o"
131-
131+
splot = ''
132132
if isinstance(f, str):
133133
f = open(f).readlines()
134134

@@ -181,8 +181,10 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o",
181181
nlen = max(len(str(min_y)), len(str(max_y))) + 1
182182

183183
if title:
184-
print_func(box_text(title, max(len(hist) * 2, len(title)), nlen))
185-
print_func()
184+
splot += print_return_str(
185+
box_text(title, max(len(hist) * 2, len(title)), nlen),
186+
return_str=return_str)
187+
splot += print_return_str('', return_str=return_str)
186188

187189
used_labs = set()
188190
for y in ys:
@@ -193,47 +195,52 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o",
193195
used_labs.add(ylab)
194196
ylab = " " * (nlen - len(ylab)) + ylab + "|"
195197

196-
print_func(ylab, end=' ')
198+
splot += print_return_str(ylab, end=' ', return_str=return_str)
197199

198200
for i in range(len(hist)):
199201
if int(y) <= hist[i]:
200-
printcolour(pch, True, colour, print_func)
202+
splot += printcolour(pch, True, colour, return_str)
201203
else:
202-
printcolour(" ", True, colour, print_func)
203-
print_func('')
204+
splot += printcolour(" ", True, colour, return_str)
205+
splot += print_return_str('', return_str=return_str)
204206
xs = hist.keys()
205207

206-
print_func(" " * (nlen + 1) + "-" * len(xs))
208+
splot += print_return_str(" " * (nlen + 1) + "-" * len(xs),
209+
return_str=return_str)
207210

208211
if xlab:
209212
xlen = len(str(float((max_y) / height) + max_y))
210213
for i in range(0, xlen):
211-
printcolour(" " * (nlen + 1), True, colour, print_func)
214+
splot += printcolour(" " * (nlen + 1), True, colour, return_str)
212215
for x in range(0, len(hist)):
213216
num = str(bins[x])
214217
if x % 2 != 0:
215218
pass
216219
elif i < len(num):
217-
print_func(num[i], end=' ')
220+
splot += print_return_str(num[i], end=' ',
221+
return_str=return_str)
218222
else:
219-
print_func(" ", end=' ')
220-
print_func('')
223+
splot += print_return_str(" ", end=' ',
224+
return_str=return_str)
225+
splot += print_return_str('', return_str=return_str)
221226

222227
center = max(map(len, map(str, [n, min_val, mean, max_val])))
223228
center += 15
224229

225230
if showSummary:
226-
print_func()
227-
print_func("-" * (2 + center))
228-
print_func("|" + "Summary".center(center) + "|")
229-
print_func("-" * (2 + center))
231+
splot += print_return_str('', return_str=return_str)
232+
splot += print_return_str("-" * (2 + center), return_str=return_str)
233+
splot += print_return_str("|" + "Summary".center(center) + "|",
234+
return_str=return_str)
235+
splot += print_return_str("-" * (2 + center), return_str=return_str)
230236
summary = "|" + ("observations: %d" % n).center(center) + "|\n"
231237
summary += "|" + ("min value: %f" % min_val).center(center) + "|\n"
232238
summary += "|" + ("mean : %f" % mean).center(center) + "|\n"
233239
summary += "|" + ("sd : %f" % sd).center(center) + "|\n"
234240
summary += "|" + ("max value: %f" % max_val).center(center) + "|\n"
235241
summary += "-" * (2 + center)
236-
print_func(summary)
242+
splot += print_return_str(summary, return_str=return_str)
243+
return splot
237244

238245

239246
def main():

bashplotlib/scatterplot.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def get_scale(series, is_y=False, steps=20):
2828
return scaled_series
2929

3030

31-
def plot_scatter(xs, ys, size=None, pch='o', colour='red', title=None, print_func=print):
31+
def plot_scatter(xs, ys, size=None, pch='o',
32+
colour='red', title=None, return_str=False):
3233
''' Scatter plot.
3334
----------------------
3435
| * |
@@ -54,19 +55,27 @@ def plot_scatter(xs, ys, size=None, pch='o', colour='red', title=None, print_fun
5455
white,aqua,pink,blue,yellow,green,red,grey,black,default,ENDC
5556
title : str
5657
title for the plot, None = not show
57-
print_func : callable
58-
function for print out a string
59-
58+
return_str : boolean
59+
return string represent the plot or print it out, default: False
6060
'''
61+
splot = ''
6162
plotted = set()
6263
cs = colour
6364

64-
if title:
65-
print_func(box_text(title, 2 * len(get_scale(xs, False, size)) + 1))
65+
if size is None:
66+
size = 13
6667

67-
print_func("-" * (2 * len(get_scale(xs, False, size)) + 2))
68+
if title:
69+
splot += print_return_str(
70+
box_text(title, 2 * len(get_scale(xs, False, size)) + 1),
71+
return_str=return_str)
72+
73+
# ====== Top line ====== #
74+
splot += print_return_str(' ' + "-" * (len(get_scale(xs, False, size)) + 2),
75+
return_str=return_str)
76+
# ====== Main plot ====== #
6877
for y in get_scale(ys, True, size):
69-
print_func("|", end=' ')
78+
splot += print_return_str("|", end=' ', return_str=return_str)
7079
for x in get_scale(xs, False, size):
7180
point = " "
7281
for (i, (xp, yp)) in enumerate(zip(xs, ys)):
@@ -75,9 +84,12 @@ def plot_scatter(xs, ys, size=None, pch='o', colour='red', title=None, print_fun
7584
plotted.add((xp, yp))
7685
if isinstance(cs, list):
7786
colour = cs[i]
78-
printcolour(point, True, colour, print_func)
79-
print_func(" |")
80-
print_func("-" * (2 * len(get_scale(xs, False, size)) + 2))
87+
splot += printcolour(point, True, colour, return_str)
88+
splot += print_return_str(" |", return_str=return_str)
89+
# ====== Bottom line ====== #
90+
splot += print_return_str(' ' + "-" * (len(get_scale(xs, False, size)) + 2),
91+
return_str=return_str)
92+
return splot
8193

8294
def _plot_scatter(f, xs, ys, size, pch, colour, title):
8395
"""

bashplotlib/utils/helpers.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
isiterable = lambda x: hasattr(x, '__iter__') or hasattr(x, '__getitem__')
1010

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

2525
colour_help = ', '.join([colour for colour in bcolours if colour != "ENDC"])
@@ -31,20 +31,23 @@ def get_colour(colour):
3131
"""
3232
return bcolours.get(colour, bcolours['ENDC'])
3333

34+
def print_return_str(text, end='\n', return_str=False):
35+
if not return_str:
36+
print(text, end=end)
37+
return text + end
3438

35-
def printcolour(text, sameline=False, colour=get_colour("ENDC"), print_func=print):
39+
def printcolour(text, sameline=False, colour=get_colour("ENDC"), return_str=False):
3640
"""
3741
Print color text using escape codes
3842
"""
3943
if sameline:
4044
sep = ''
4145
else:
4246
sep = '\n'
43-
if print_function == print:
44-
sys.stdout.write(get_colour(colour) + text + bcolours["ENDC"] + sep)
45-
else:
46-
print_func(get_colour(colour) + text + bcolours["ENDC"] + sep)
47-
47+
if colour == 'default' or colour == 'ENDC' or colour is None:
48+
return print_return_str(text, sep, return_str)
49+
return print_return_str(get_colour(colour) + text + bcolours["ENDC"],
50+
sep, return_str)
4851

4952
def drange(start, stop, step=1.0, include_stop=False):
5053
"""
@@ -70,7 +73,7 @@ def box_text(text, width, offset=0):
7073
"""
7174
Return text inside an ascii textbox
7275
"""
73-
box = " " * offset + "-" * (width+2) + "\n"
76+
box = " " * offset + "-" * (width + 2) + "\n"
7477
box += " " * offset + "|" + text.center(width) + "|" + "\n"
75-
box += " " * offset + "-" * (width+2)
78+
box += " " * offset + "-" * (width + 2)
7679
return box

0 commit comments

Comments
 (0)