From c4b083ae6addcfc853ad6cac20d148e112064c76 Mon Sep 17 00:00:00 2001 From: Corrado Avolio Date: Fri, 7 Dec 2018 10:42:17 +0100 Subject: [PATCH 1/2] Unambiguous abbreviations of x-labels in histogram --- bashplotlib/histogram.py | 5 +++-- bashplotlib/utils/helpers.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/bashplotlib/histogram.py b/bashplotlib/histogram.py index 43b7dd3..89a10db 100644 --- a/bashplotlib/histogram.py +++ b/bashplotlib/histogram.py @@ -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): diff --git a/bashplotlib/utils/helpers.py b/bashplotlib/utils/helpers.py index bfffe46..cf209ee 100644 --- a/bashplotlib/utils/helpers.py +++ b/bashplotlib/utils/helpers.py @@ -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 From 6fa4526a82f321a0a1d034dd5682827a5b7d41c6 Mon Sep 17 00:00:00 2001 From: Corrado Avolio Date: Fri, 7 Dec 2018 11:01:43 +0100 Subject: [PATCH 2/2] Added sample with x-labels. --- examples/sample.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/sample.sh b/examples/sample.sh index 02985ed..08702e5 100755 --- a/examples/sample.sh +++ b/examples/sample.sh @@ -22,6 +22,9 @@ hist --file "${dir}/data/exp.txt" --colour blue echo 'changing the shape of the point' hist --file "${dir}/data/exp.txt" --pch . +echo 'adding x-labels' +hist --file "${dir}/data/exp.txt" --pch . --xlab + #echo 'using stdin' #curl -sL https://dl.dropbox.com/u/49171662/example.txt | hist