☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

Update bokeh legend after showing it

Community Support
albertino87
albertino87

Hi all,
I’m unable to update the labels of a legend after it has been shown. I initialize the plot with 10 empty lines. I don’t know however in advance which curves the user will decide to plot because there are more then ten possible curves.

in the init method of my class PakTab I have:

class PakTab:

    def __init__(self):
        self.__signalsCDS = dict()
        self.__graph_Signal = figure(title="Sensor", height=400)
        self.__graph_Signal.xaxis.axis_label = 'Time (s)'
        for i in range(0, 10):
            self.__signalsCDS['LINE'+str(i)] = ColumnDataSource(dict(x=[0], y=[0]))
            self.__graph_Signal.line(source=self.__signalsCDS['LINE'+str(i)], x='x', y='y', legend_label=str(i),
                                     color=Category10[10][i])
        self.__graph_Signal.legend.location = 'bottom_right'
        self.__graph_Signal.legend.click_policy = 'hide'

when I plot a curve i call a method, I added .legend.visible=False, and True to see if that updated the graph but it doesn’t:

def __plot_signal(self):
    sensor = self.__pak_info.get_sensor_metadata(self.__selected_sensor)
    if sensor:
        sensor.get_sensor_data()
        i = next(self.__iterator)
        self.__graph_Signal.legend.items[i].label['value'] = sensor.label
        self.__signalsCDS['LINE' + str(i)].data = dict(x=sensor.create_pcm_time(), y=sensor.values)
        self.__graph_Signal.legend.visible = False
        self.__graph_Signal.legend.visible = True

do you know how I can achieve this?
Many thanks

⌁   Link to post
Bryan
Bryan

Due to technical limitations, Bokeh can only auto-magically detect specific kinds of changes. In general, Bokeh can detect when a property changes entirely:

item.label = foo     # Good: change entire .label property value

But can not detect changes “inside” mutable values:

label['value'] = bar # Bad: modify a dict in-place 

You will need to find a way to set .label to a whole new value, not merely tinker with the existing dict’s internals.

As an aside, since I used to teach Python extensively for many years, I also feel compelled to offer a little unsolicited advice: there’s really almost no reason to ever user double-underscore prefixes. In particular, since this is Python and everything is always accessible, using them in no way prevents anyone who wants to find and access these attributes from doing so. All they do is make code much harder to read, for no benefit.

⌁   Link to post

End of archived discussion · Replies are disabled