Legend outside plot area won't resize

I have a plot with a legend right, outside of the plot area.
I want to change the label of the legend during a callback but observe that the ratio of the plot area and legend will not adapt automatically according to the new length of the legend text.

Here ist my exemplary code:

from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.models import Legend, LegendItem, Button
from bokeh.layouts import column

# Prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# Create a new plot
p = figure(x_axis_label="x", y_axis_label="y", toolbar_location="above", height=200)

# Create an empty legend right, outside of the plotting area:
p.add_layout(Legend(items=[]), "right")
p.legend.click_policy="hide" # Clicking on the legend after callback "repairs" the layout.

# Add renderer:
line = p.line(x, y)
p.legend[0].items.append(LegendItem(label="Temp.", renderers=[line]))

# Define callback with triggering button:
button = Button(label="Click")

def callback():
    p.legend.items[0].label = {'value': "Longer legend"}

button.on_click(callback)

# Define layout:
my_layout = column(button, p)

curdoc().add_root(my_layout)

Here is how the app looks like at the beginning:
ini

After triggering the callback by clicking the button, it looks like as follows:
After callback

The legend is overlapping the plot area because the legend label is longer.
I would expect that the ratio of the plot area and legend adapt automatically to conserve the padding.

This can be achieved by clicking twice on the legend to hide and show back the curve.
Here is how the app looks like after clicking the legend twice:
After clicking the legend twice

Is there a way I can force the aspect ratio to update automatically after changing legend label?

By the way, another question: is it possible to set manually the width of the plotting area, letting the legend expand right at will?

My system:
Windows 10
Python 3.12.0
Bokeh 3.3.0

GitHub issue created:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.