Legend with two glyph in the same line with different colors

Dear all,

I write bokeh for around a month now. I would like to know if it is possible to add a Legend with two glyph with different colors.

So you have a very helpful example here:

If I wanted to add a Legend for the two boxes I would normally do:

# boxes
box1 = p.vbar(latest_queries, 0.7, q2.score, q3.score, fill_color="#E08E79", line_color="black")
box2 = p.vbar(latest_queries, 0.7, q1.score, q2.score, fill_color="#3B8686", line_color="black")

And then later on:

legend = Legend(items=[('Latest results', [box1,box2]), location="bottom_left")

However this will result in a Legend with one box with only the first color.
Is there a way to create a custom Legend with two glyph, but in the same line and with the same text?

Hi katamarandameko,

One way to do it is to add the legend argument when you declare the glyphs. Like this:

p.vbar(cats, 0.7, q2.score, q3.score, fill_color="#E08E79", line_color="black", legend='thing 1')
p.vbar(cats, 0.7, q1.score, q2.score, fill_color="#3B8686", line_color="black", legend='thing 2')

You could also manually build the legend out of LegendItems you define, but it’d need one LegendItem per renderer:

box1 = p.vbar(cats, 0.7, q2.score, q3.score, fill_color="#E08E79", line_color="black")
box2 = p.vbar(cats, 0.7, q1.score, q2.score, fill_color="#3B8686", line_color="black")

legend = Legend(
    items=[
        LegendItem(label='thing 1', renderers=[box1]),
        LegendItem(label='thing 2', renderers=[box2])
    ]
)

I’m not sure about adding your ‘Latest Results’ title, so I’ll leave that part for others to answer. :slight_smile:

I believe it does not work differently than the example that I have provided.

It is my fault that I haven’t included an image of how I would like the result to be:

example

There are two boxes with different color side by side and then one legend text.

This is not currently possible with Legend. The only suggestion I have is to draw things by hand with text and rect etc.

1 Like

Thank you very much. Both of you.

1 Like