Skip to content

Commit 04819d7

Browse files
BenjaminCharmesml-evs
authored andcommitted
move legend outside plot by default and add a maximum label length
1 parent 965f530 commit 04819d7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pydatalab/src/pydatalab/bokeh_plots.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,23 @@ def selectable_axes_plot(
279279
else:
280280
label = df_.index.name if len(df) > 1 else ""
281281

282+
if label is None:
283+
label = ""
284+
285+
if label and len(label) > 15:
286+
if "." in label:
287+
name, ext = label.rsplit(".", 1)
288+
if len(ext) < 6:
289+
available = 15 - len(ext) - 4
290+
if available > 3:
291+
label = f"{name[:available]}...{ext}"
292+
else:
293+
label = f"{label[:12]}..."
294+
else:
295+
label = f"{label[:12]}..."
296+
else:
297+
label = f"{label[:12]}..."
298+
282299
source = ColumnDataSource(df_)
283300

284301
if color_options:
@@ -370,6 +387,21 @@ def selectable_axes_plot(
370387
p.legend.click_policy = "hide"
371388
if len(df) <= 1:
372389
p.legend.visible = False
390+
else:
391+
legend_items = p.legend.items
392+
p.legend.visible = False
393+
394+
from bokeh.models import Legend
395+
396+
external_legend = Legend(
397+
items=legend_items,
398+
click_policy="hide",
399+
background_fill_alpha=0.8,
400+
label_text_font_size="9pt",
401+
spacing=1,
402+
margin=2,
403+
)
404+
p.add_layout(external_legend, "right")
373405

374406
if not skip_plot:
375407
plot_columns.append(p)

0 commit comments

Comments
 (0)