Skip to content

Commit aaf1091

Browse files
committed
updated horiz histogram
1 parent 59bd6db commit aaf1091

File tree

3 files changed

+89
-68
lines changed

3 files changed

+89
-68
lines changed

bashplotlib/horizontal_histogram.py

Lines changed: 49 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -195,62 +195,10 @@ def plot_horiz_hist(f, width=20.0, bincount=None, binwidth=None, pch="o", colour
195195

196196
print(" " * (nlen + 1) + "-" * len(ys))
197197

198-
used_labs = set()
199-
# print(" " * (binlen + 1))
200-
y_master_label = " " * (binlen + 1)
201-
202-
power_of_ten = 1
203-
while power_of_ten < max_y:
204-
power_of_ten *= 10
205-
206-
current_power = 10
207-
while index * 10
208-
for y in ys:
209-
if y > current_power * 10:
210-
current_power *= 10
211-
if y < current_power:
212-
y_master_label += str(y) + " "
213-
else:
214-
y_master_label += str(y/current_power) + " "
215-
while power_of_ten > 0:
216-
labels = []
217-
218-
# print(ys)
219-
for lab in labels:
220-
ylab = lab
221-
if ylab in used_labs:
222-
ylab = ""
223-
else:
224-
used_labs.add(ylab)
225-
y_master_label += " " + ylab
226-
print(y_master_label)
227-
228-
# print(ylab, end=' ')
229-
230-
# for i in range(len(hist)):
231-
# if int(y) <= hist[i]:
232-
# printcolour(pch, True, colour)
233-
# else:
234-
# printcolour(" ", True, colour)
235-
# print('')
236-
# xs = hist.keys()
237-
238-
# print(" " * (nlen) + "+" + "-" * len(xs))
239-
240-
# if xlab:
241-
# labels = abbreviate([str(y) for y in ys])
242-
# xlen = len(labels[0])
243-
# for i in range(0, xlen):
244-
# printcolour(" " * (nlen + 1), True, colour)
245-
# for x in range(0, len(hist)):
246-
# num = labels[x]
247-
# if x % 2 != 0:
248-
# pass
249-
# elif i < len(num):
250-
# print(num[i], end=' ')
251-
# else:
252-
# print(" ", end=' ')
253-
# print('')
198+
ys.reverse()
199+
print(get_y_label(ys, binlen))
200+
201+
254202
if ytitle:
255203
full_title = "y: "+ ytitle
256204
print(" " * ((nlen + 1) + len(xs) - len(full_title)) + full_title)
@@ -266,17 +214,52 @@ def plot_horiz_hist(f, width=20.0, bincount=None, binwidth=None, pch="o", colour
266214
stats = ["observations: %d" % n, "min value: %f" % min_val,
267215
"mean : %f" % mean, "std dev : %f" % sd, "max value: %f" % max_val]
268216
print(box_text(stats, max(len(hist) * 2, len(title)), nlen))
269-
# print("-" * (2 + center))
270-
# print("|" + "Summary".center(center) + "|")
271-
# print("-" * (2 + center))
272-
# summary = "|" + ("observations: %d" % n).center(center) + "|\n"
273-
# summary += "|" + ("min value: %f" % min_val).center(center) + "|\n"
274-
# summary += "|" + ("mean : %f" % mean).center(center) + "|\n"
275-
# summary += "|" + ("std dev : %f" % sd).center(center) + "|\n"
276-
# summary += "|" + ("max value: %f" % max_val).center(center) + "|\n"
277-
# summary += "-" * (2 + center)
278-
# print(summary)
279217

218+
def get_y_label(ys, binlen):
219+
y_master_label = ""
220+
ys_copy = ys[:]
221+
222+
used_labels = set()
223+
for (i, y) in enumerate(ys_copy):
224+
y = int(y)
225+
if y in used_labels:
226+
ys_copy[i] = 0
227+
else:
228+
ys_copy[i] = int(y)
229+
used_labels.add(y)
230+
231+
zeros = len(ys)*[0]
232+
continue_build = True
233+
234+
235+
236+
while continue_build:
237+
y_master_label += " " * (binlen + 2)
238+
power_of_ten = 10
239+
240+
for (i, y) in enumerate(ys_copy):
241+
if y < 1:
242+
if zeros[i] > 0:
243+
y_master_label += "0"
244+
else:
245+
y_master_label += " "
246+
else:
247+
while y >= power_of_ten:
248+
power_of_ten *= 10
249+
zeros[i] += 1
250+
add_val = y // (power_of_ten/10)
251+
y_master_label += str(int(add_val))
252+
# print(ys_copy)
253+
ys_copy[i] -= add_val * (power_of_ten/10)
254+
zeros[i] -= 1
255+
256+
continue_build = False
257+
for y in ys_copy:
258+
if y > 0:
259+
continue_build = True
260+
y_master_label += "\n"
261+
break
262+
return y_master_label
280263

281264
def main():
282265

scratch.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# scratch.py
22
from bashplotlib.horizontal_histogram import plot_horiz_hist
3+
from bashplotlib.histogram import plot_hist
4+
import random
5+
6+
nums = []
7+
for i in range(1,100):
8+
nums.append(random.randint(1,100))
39

4-
nums = [0,10,15,5,5,5,5,5,5,5,5,10,10,10]
510
height = 20
611
bincount = 10
712
binwidth = 1
@@ -13,7 +18,7 @@
1318
yStart = True
1419

1520

16-
plot_horiz_hist(
21+
plot_hist(
1722
nums,
1823
height,
1924
bincount,

scratch2.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# scratch.py
2+
from bashplotlib.horizontal_histogram import plot_horiz_hist
3+
4+
import random
5+
6+
nums = []
7+
for i in range(1,100):
8+
nums.append(random.randint(1,100))
9+
10+
height = 200
11+
bincount = 10
12+
binwidth = 1
13+
char = 'o'
14+
color = 'default'
15+
title = 'My Bar Chart'
16+
displayXLabel = True
17+
showSum = True
18+
yStart = True
19+
20+
21+
plot_horiz_hist(
22+
nums,
23+
height,
24+
bincount,
25+
binwidth,
26+
char,
27+
color,
28+
title,
29+
displayXLabel,
30+
showSum,
31+
yStart,
32+
xtitle="My x",
33+
ytitle="My y")

0 commit comments

Comments
 (0)