Single Legend for GlyphRenderers on Multiple Plots
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:7
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
It would be very interesting if this resource exists. It would also help me a lot.