I have an interactive Bokeh plot that's able to hide certain circle plots when clicking on the Legend. Now when I disable a plot by clicking on it, the plotted circles disappear but the annotations remain. Could someone explain to me how I can toggle those on/off all together or does anyone have a quick fix?

with

Here's a picture when it's toggled off:

without

I plot the circles + legend with the following code:

q.circle('lng', 'lat', source = source2, name='vri', color='red', size=5, hover_line_color="black", legend_label = 'VRI')

vri_labels = LabelSet(x='lng', y='lat', text='kruispuntn', x_offset=5, y_offset=5, source=source2, text_font_size = '10pt')

q.legend.location = "bottom_left"
q.legend.click_policy="hide"

q.add_layout(vri_labels)

show(q)

score:5

Accepted answer

You can link the visible property via a CustomJS callback:

from bokeh.io import show
from bokeh.models import ColumnDataSource, LabelSet, CustomJS
from bokeh.plotting import figure

p = figure()
cds = ColumnDataSource(data=dict(x=[0, 1], y=[0, 1], z=[1, 0]))

for var, params in [('y', {}),
                    ('z', {'color': 'green'})]:
    renderer = p.circle('x', var, source=cds, legend_label=var, size=20, **params)
    label_set = LabelSet(x='x', y=var, text=var, source=cds, x_offset=5, y_offset=5)
    p.add_layout(label_set)
    renderer.js_on_change('visible', CustomJS(args=dict(ls=label_set),
                                              code="ls.visible = cb_obj.visible;"))

p.legend.click_policy = 'hide'

show(p)

Related Query