question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Single Legend for GlyphRenderers on Multiple Plots

See original GitHub issue

I have tried to make this work

from bokeh.models import ColumnDataSource, CDSView, IndexFilter
from bokeh.palettes import Reds3
from bokeh.layouts import row
from bokeh.plotting import curdoc, figure
from bokeh.models.annotations import Legend
import numpy as np

plot_1 = figure(
    width=300,
    height=300,
    tools="pan,box_zoom,box_select,wheel_zoom,tap,save,reset,crosshair",
    toolbar_location='left',
)

plot_2 = figure(
    width=430,
    height=300,
    tools="pan,box_zoom,box_select,wheel_zoom,tap,save,reset,crosshair",
    toolbar_location='left',
)

x = list(range(0,10))
y = np.random.random_integers(200, size=(10))
l = np.random.random_integers(3, size=(10))

x2 = list(range(0,10))
y2 = np.random.random_integers(200, size=(10))
l2 = np.random.random_integers(3, size=(10))

source = ColumnDataSource(data=dict(x=x, y=y, x2=x2, y2=y2))

view_1 = CDSView(
    source=source,
    filters=[IndexFilter([0, 1, 2, 3])]
)

view_2 = CDSView(
    source=source,
    filters=[IndexFilter([5, 6, 7])]
)

view_3 = CDSView(
    source=source,
    filters=[IndexFilter([8, 9, 4])]
)

circle_1 = plot_1.circle(
    x='x',
    y='y',
    size=5,
    fill_alpha=1,
    line_color=None,
    fill_color=Reds3[0],
    source=source,
    view=view_1,
)

circle_2 = plot_1.circle(
    x='x',
    y='y',
    size=5,
    fill_alpha=1,
    line_color=None,
    fill_color=Reds3[1],
    source=source,
    view=view_2,
)

circle_3 = plot_1.circle(
    x='x',
    y='y',
    size=5,
    fill_alpha=1,
    line_color=None,
    fill_color=Reds3[2],
    source=source,
    view=view_3,
)

view_4 = CDSView(
    source=source,
    filters=[IndexFilter([0, 1, 2, 3])]
)

view_5 = CDSView(
    source=source,
    filters=[IndexFilter([5, 6, 7])]
)

view_6 = CDSView(
    source=source,
    filters=[IndexFilter([8, 9, 4])]
)

circle_4 = plot_2.circle(
    x='x',
    y='y',
    size=5,
    fill_alpha=1,
    fill_color=Reds3[0],
    line_color=None,
    source=source,
    view=view_4,
)

circle_5 = plot_2.circle(
    x='x',
    y='y',
    size=5,
    fill_alpha=1,
    fill_color=Reds3[1],
    line_color=None,
    source=source,
    view=view_5,
)

circle_6 = plot_2.circle(
    x='x',
    y='y',
    size=5,
    fill_alpha=1,
    fill_color=Reds3[2],
    line_color=None,
    source=source,
    view=view_6,
)

legend = Legend(
    items=[
        ("LEGEND 1", [circle_4]),
        ("LEGEND 2", [circle_5]),
        ("LEGEND 3", [circle_3, circle_6]),  # renderers for several plots
    ],
    location=(10, 0)
)
plot_2.add_layout(legend, 'right')
plot_2.legend.click_policy = 'hide'

curdoc().add_root(row([plot_1, plot_2]))

I think this is not possible because the legend is usually added to one plot plot.add_layout(legend, 'right'). Anyway I have run an example and the glyphs have become invisible on both plots. But I get this error on JavaScript Cannot read property 'draw_legend' of undefined and the color on the legend box is not visible.

So I write this as a feature request. I think It would be useful to have more flexibility on the Legends objects.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:7
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
johnrbnsncommented, Apr 7, 2019

Not a high priority but we can try to consider it at some point

I’d like to say, one of the main reasons I chose Bokeh was your linked plot support. Having large amounts of data on multiple plots, the interactive legend working between them would be a huge benefit for me.

4reactions
matheusrmorgadocommented, Apr 11, 2019

Not a high priority but we can try to consider it at some point

It would be very interesting if this resource exists. It would also help me a lot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I make a single legend for many subplots?
I have noticed that none of answers displays an image with a single legend referencing many curves in different subplots, so I have...
Read more >
Building a Legend for hbar plot - Bokeh Discourse
I'm trying to build a legend for the above hbar plot. The data used to generate the plot is stored in two columns,...
Read more >
How to Create a Single Legend for All Subplots in Matplotlib?
The legend() can be customized and adjusted anywhere inside or outside the graph by placing it at various positions. Sometimes it is necessary ......
Read more >
VisualizationBokeh - | notebook.community
In [1]:. from bokeh.plotting import figure, output_file, show. In [2]: ... add a line renderer with legend and line thickness p.line(x, y, legend="Temp....
Read more >
Generate a single legend for multiple plots (e.g. DensityPlot)
Legend can be generated separately with ArrayPlot using common color function. For better labels you can use MaTeX. (* set default color for ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found