简体   繁体   中英

Bokeh - legend outside the plot

There is plenty of issues like this one, but I couldn't find one with the approach I'm looking to solve it.

In bokeh we cannot move a legend outside the plot, we have to create one. If we try nowadays to move the legend from inside to outside the legend dissapears. In the documentation (and in the solutions provided I have found in SO like 1 , 2 , and this solution does the copy but seems to use outdated fuctions), to plot an outside legend you need to create that legend from your data, not from the plot.

However, is it possible to access the existing legend inside the plot, copying it, and creating the outside legend.. with that copy?

I'm looking for this approach because I developed a function that creates this plot, with two x-axis (date and category per date) and two y-axis (percentage and integers), so creating a legend that fits with every color, every line-style, every time the categories changes, for every plot.. it's kind of complicated. So, as the inside legend is just perfect, I thought that just copying it to the outside new legend would be pretty straightforward, however I have not been able to do so.

Any suggestions?

Something like this?

from bokeh.io import show
from bokeh.models import Legend
from bokeh.plotting import figure

p = figure(tools=[])
p.circle(x=[0, 1], y=[0, 1], size=10, legend_label='Circle')

legend = p.legend[0]
p.center = [item for item in p.center if not isinstance(item, Legend)]
p.add_layout(legend, 'right')

show(p)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM