☰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

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

Community Support
nodice
nodice

Hello,

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(
        tools="pan,wheel_zoom,reset,box_select,lasso_select,save,tap",
        active_scroll='wheel_zoom')

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)(
            "petal_length",
            "sepal_width",
            color='color',
            nonselection_fill_color='color',
            nonselection_line_color='color',
            nonselection_fill_alpha=0.5,
            nonselection_line_alpha=0.5,
            source=flower_source, fill_alpha=1, size=12,
            view=view,
            legend_label=sp)

p.legend.click_policy = 'hide'

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

show(p)

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!

⌁   Link to post

End of archived discussion · Replies are disabled