Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Unambiguous abbreviations of x-labels in histogram
  • Loading branch information
Corrado Avolio committed Dec 7, 2018
commit c4b083ae6addcfc853ad6cac20d148e112064c76
5 changes: 3 additions & 2 deletions bashplotlib/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,12 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
print(" " * (nlen + 1) + "-" * len(xs))

if xlab:
xlen = len(str(float((max_y) / height) + max_y))
labels = abbreviate([str(b) for b in bins])
xlen = len(labels[0])
for i in range(0, xlen):
printcolour(" " * (nlen + 1), True, colour)
for x in range(0, len(hist)):
num = str(bins[x])
num = labels[x]
if x % 2 != 0:
pass
elif i < len(num):
Expand Down
12 changes: 12 additions & 0 deletions bashplotlib/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def drange(start, stop, step=1.0, include_stop=False):
r = round(r, 10)


def abbreviate(labels, rfill=' '):
"""
Abbreviate labels without introducing ambiguities.
"""
max_len = max(len(l) for l in labels)
for i in range(1, max_len):
abbrev = [l[:i].ljust(i, rfill) for l in labels]
if len(abbrev) == len(set(abbrev)):
break
return abbrev


def box_text(text, width, offset=0):
"""
Return text inside an ascii textbox
Expand Down