From ed1be8816a8af400a02a84cff7d633d386492fec Mon Sep 17 00:00:00 2001 From: frederik Date: Tue, 16 Jul 2024 19:50:03 +0200 Subject: [PATCH] labels with wide letters should wrap sooner so they are not cut off --- scripts/daily_plot.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/scripts/daily_plot.py b/scripts/daily_plot.py index 805cd68..466662a 100755 --- a/scripts/daily_plot.py +++ b/scripts/daily_plot.py @@ -46,6 +46,17 @@ def show_values_on_bars(ax, label): ax.text(x, y, value, bbox=bbox, ha='center', va='center', size=9, color='darkgreen') +def wrap_width(txt): + # try to estimate wrap width + w = 16 + for c in txt: + if c in ['M', 'm', 'W', 'w']: + w -= 0.33 + if c in ['I', 'i', 'j', 'l']: + w += 0.33 + return round(w) + + def create_plot(df_plt_today, now, is_top=None): if is_top is not None: readings = 10 @@ -97,7 +108,7 @@ def create_plot(df_plt_today, now, is_top=None): show_values_on_bars(axs[0], confmax) # Try plot grid lines between bars - problem at the moment plots grid lines on bars - want between bars - yticklabels = ['\n'.join(textwrap.wrap(ticklabel.get_text(), 16)) for ticklabel in plot.get_yticklabels()] + yticklabels = ['\n'.join(textwrap.wrap(ticklabel.get_text(), wrap_width(ticklabel.get_text()))) for ticklabel in plot.get_yticklabels()] # Next two lines avoid a UserWarning on set_ticklabels() requesting a fixed number of ticks yticks = plot.get_yticks() plot.set_yticks(yticks)