Interactive Legend with only one patches object

Hi, I’m wondering if there is a way to get a plot legend to turn on/off its correspondingly colored polygons when they all belong to the same Patches object.

See my super simple working example below:

from bokeh.io import show
from bokeh.models import ColumnDataSource,CustomJS, widgets, LinearColorMapper
from bokeh.palettes import RdBu6
from bokeh.plotting import figure
from bokeh.layouts import layout
import numpy as np

source = ColumnDataSource(dict(
    xs=[[1,2,2], [1,2,2], [3,4,4], [3,4,4]],
    ys=[[3,3,4], [1,1,2], [3,3,4], [1,1,2]],
    s1=[0, 50, 75, 50],
    label_1=['Blue', 'Orangy', 'Red', 'Orangy'],
    
))

cmap1 = LinearColorMapper(palette='RdBu6', low = 0, high = 75)

p = figure(x_range=(0, 7), y_range=(0, 5), plot_height=300)

patches = p.patches( xs='xs', ys='ys', fill_color={'field':'s1','transform':cmap1}
                    , legend_field='label_1', source=source)

p.legend.click_policy='hide'

show(p)

The legend.click_policy lets me hide the entire patches object, but I’d like to toggle individual legend entries. I’ve tried implementing a checkbox JS callback as outlined here: https://stackoverflow.com/questions/45380781/bokeh-interactive-legend-hide-multiple-glyphs

However, I’m running into snags because the setup above uses methods that exist for the Bar object but not for the Patches object. Before I go diving into this approach but for Patches, not Bars, I’m wondering if this is the right approach or there’s something else entirely I can do. My only caveat is that I really can’t break up my patch objects into multiple glyphs - my real life version (not simplified example) is far too big to do that.

Any help greatly appreciated :slight_smile:

I’m wondering if there is a way to get a plot legend to turn on/off its correspondingly colored polygons when they all belong to the same Patches object.

I am afraid there is not. The interactive legend capability only works on an entire glyph at a time (clicking just toggles the visible property for the glyph).

The only suggestions I can think of are not simple, e.g. draw your own legend (glyphs and labels) by hand, then add event callbacks to do whatever filtering is necessary to hide the appropriate subsets of the data (which is also not simple, I think you’d have to maintain an entire column of alpha values so that you could selectively set a subset of alpha values to zero).

1 Like

Ooof that is not ideal. Will file that under “nice to have.” Thanks for response.

Definitely amenable to discuss ways to add it in a GitHub issue especially if there is interest to collaborate on implementation. It’s one of those things that’s going to be somewhat tedious to sort out the cases, and given all the things that intersect around glyph renderers, there’s also a higher chance that there will be combinations or interactions that don’t work so well together.