bokeh icon indicating copy to clipboard operation
bokeh copied to clipboard

[BUG] legend click policy 'hide' has unexpected behavior on automatic-grouping

Open mosc9575 opened this issue 4 years ago • 2 comments
trafficstars

ALL software version info

bokeh: v2.3.0 python: 3.6+

Description of expected behavior and the observed behavior

I was checking the automatic-grouping using this example from the docu.

The data:

import pandas as pd

from bokeh.io import show, outut_notebook
from bokeh.models import ColumnDataSource
from bokeh.palettes import RdBu3
from bokeh.plotting import figure
outut_notebook()

c1 = RdBu3[2] # red
c2 = RdBu3[0] # blue
source = pd.DataFrame(dict(
    x=[1, 2, 3, 4, 5, 6],
    y=[2, 1, 2, 1, 2, 1],
    color=[c1, c2, c1, c2, c1, c2],
    label=['hi', 'lo', 'hi', 'lo', 'hi', 'lo']
))

How it is now:

I just added one line to change the click policy of the legend. Then clicking on the legend, all elements were hidden.

p = figure(x_range=(0, 7), y_range=(0, 3), plot_height=300, tools='save')
# legend field matches the column in the source
p.circle( x='x', y='y', radius=0.5, color='color', legend_group='label', source=source)
p.legend.click_policy="hide"
show(p)

real

Expectation

I was expecting, that the grouping works more like this:

p = figure(x_range=(0, 7), y_range=(0, 3), plot_height=300, tools='save')
# legend field matches the column in the source
grouper = source.groupby('label')
for name in source['label'].unique():
    p.circle( x='x', y='y', radius=0.5, color='color', legend_label=name, source=grouper.get_group(name))
p.legend.click_policy="hide"
show(p)

expected

mosc9575 avatar Mar 04 '21 10:03 mosc9575

Your expectation is correct, but the current design doesn't allow for this. This is because click policies work on renderers and don't have access to subsets of indices the grouping otherwise implies. We will need to rethink this from grounds up.

mattpap avatar Mar 04 '21 10:03 mattpap

This came up again on Discourse: https://discourse.bokeh.org/t/legend-field-multiple-data-sources-click-policy-is-click-policy-supposed-to-apply-to-entire-data-source-rather-than-just-one-legend-item/11342

FWIW a shorter-term "solution" would be to implement https://github.com/bokeh/bokeh/issues/12185 for legend click events, so that users could, e.g. update an alpha column according to their individual needs.

I'm honestly not sure what a "real" solution here might look like, let alone a minimally invasive one. We can't update data like an alpha column ourselves. But maybe a filter could applied internally.

bryevdv avatar Mar 08 '24 18:03 bryevdv