Specify legend glyph color, when each plotted glyph has multiple colors


I am creating a scatter plot with code similar to this:

from bokeh.models import Legend, LegendItem, ColumnDataSource, CDSView, GroupFilter
from bokeh.palettes import Category10_3
from bokeh.plotting import figure, show
from bokeh.sampledata.iris import flowers
from bokeh.transform import factor_cmap, factor_mark
from bokeh.io import curdoc
from bokeh.models.widgets import CheckboxGroup
from bokeh.layouts import column

import numpy as np

def assign_color(x):
    if 'setosa' in x.species:
        return Category10_3[np.random.randint(0,3)]
    elif 'versicolor' in x.species:
        return Category10_3[np.random.randint(0,3)]
    elif 'virginica' in x.species:
        return Category10_3[np.random.randint(0,3)]

flowers['color'] = flowers.apply(assign_color, axis=1)
flower_source = ColumnDataSource(flowers)

SPECIES = ['setosa', 'versicolor', 'virginica']
MARKERS = ['x', 'circle', 'triangle']

p = figure(

fcmap = factor_cmap('species', Category10_3, SPECIES)

for sp, mark in zip(SPECIES, MARKERS):
    view = CDSView(source=flower_source,
            filters=[GroupFilter(column_name='species', group=sp)])
    r = getattr(p, mark)(
            source=flower_source, fill_alpha=1, size=12,

p.legend.click_policy = 'hide'

p.x_range.renderers = [r] 
p.y_range.renderers = [r] 


Each species category can have multiple colors assigned (obviously, it isn’t random as it is in this example).

When the legend is rendered, you get one of the colors for each glyph, but that is confusing. I have a separate, static legend that explains what the colors are (as explained here).

Is it possible to assign a single color to the displayed legend glyphs associated with p.legend after the fact?

I would like to use the actual legend associated with p.legend instead of creating a “fake” one, since I would like to use the built in “hide” feature.

Thank you!

IMO the cleanest way to do that would be to manually create a legend and manually link the necessary properties together to replicated the hiding feature.

Thank you. Is there an example you can point me to? I’m afraid my knowledge is not great enough at this point to tackle it without some guidance.

On manually creating legends: https://docs.bokeh.org/en/latest/docs/user_guide/annotations.html#manual-legends
On linking properties: https://docs.bokeh.org/en/latest/docs/user_guide/interaction/linking.html#linked-properties

You’re interested in the visible property of glyph renderers (objects returned by the glyph methods such as p.circle).