bokeh icon indicating copy to clipboard operation
bokeh copied to clipboard

[FEATURE] enable linebreaks in legend labels

Open mosc9575 opened this issue 2 years ago • 2 comments

Problem description

Sometimes I have legend labels which are very long and it would be nice to have the option to add a line break (to control the length of the entries). Because this is possible for other labels (thanks to this commit [#11578]) I hope this is also easy to enable for legends.

Example

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import HoverTool
output_notebook()

p = figure(match_aspect=True, width=400, height=400)

# invisble rect to scale the y-axis as workaround
w_h = 2
p.rect(x=0, y=0, width=w_h, height=w_h, alpha=0)

p.wedge(
    x=0,
    y=0,
    radius=1,
    start_angle=0,
    end_angle=3.1415,
    color='blue',
    legend_label="Short legend item",
)
p.wedge(
    x=0,
    y=0,
    radius=1,
    start_angle=3.1415,
    end_angle=6.282,
    legend_label="Long legend item\nLinebreaks would be nice.",
    color='yellow',
)

show(p)

no legend line break

In this example you can see, that the second legend item is long and the \n-newline character is replaced by a whitespace. My wish is to start a new line without the box for the second part of the label.

mosc9575 avatar Aug 25 '22 11:08 mosc9575

This was supposed to be possible after PR #11949, but it needs a little more attention.

mattpap avatar Aug 25 '22 13:08 mattpap

Hi! Thanks for you work @mattpap.

just to add to this (with bokeh==3.0.0.dev13):

p.legend.title='Long legend item\nLinebreaks would be nice.'

does also not line break. It just does a space. So I guess this may be so for all text around legends.

bokeh_plot_line_breaks

broeder-j avatar Aug 26 '22 12:08 broeder-j