Skip to content

Commit 0a4fffc

Browse files
authored
Merge pull request #45 from corrado9999/master
Unambiguous abbreviations of x-labels in histogram
2 parents 23319fe + 6fa4526 commit 0a4fffc

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

bashplotlib/histogram.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,12 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
184184
print(" " * (nlen + 1) + "-" * len(xs))
185185

186186
if xlab:
187-
xlen = len(str(float((max_y) / height) + max_y))
187+
labels = abbreviate([str(b) for b in bins])
188+
xlen = len(labels[0])
188189
for i in range(0, xlen):
189190
printcolour(" " * (nlen + 1), True, colour)
190191
for x in range(0, len(hist)):
191-
num = str(bins[x])
192+
num = labels[x]
192193
if x % 2 != 0:
193194
pass
194195
elif i < len(num):

bashplotlib/utils/helpers.py

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ def drange(start, stop, step=1.0, include_stop=False):
6464
r = round(r, 10)
6565

6666

67+
def abbreviate(labels, rfill=' '):
68+
"""
69+
Abbreviate labels without introducing ambiguities.
70+
"""
71+
max_len = max(len(l) for l in labels)
72+
for i in range(1, max_len):
73+
abbrev = [l[:i].ljust(i, rfill) for l in labels]
74+
if len(abbrev) == len(set(abbrev)):
75+
break
76+
return abbrev
77+
78+
6779
def box_text(text, width, offset=0):
6880
"""
6981
Return text inside an ascii textbox

examples/sample.sh

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ hist --file "${dir}/data/exp.txt" --colour blue
2222
echo 'changing the shape of the point'
2323
hist --file "${dir}/data/exp.txt" --pch .
2424

25+
echo 'adding x-labels'
26+
hist --file "${dir}/data/exp.txt" --pch . --xlab
27+
2528
#echo 'using stdin'
2629
#curl -sL https://dl.dropbox.com/u/49171662/example.txt | hist
2730

0 commit comments

Comments
 (0)