I thought that it was not possible to add a legend outside of the plot area unless the legend is created directly (as stated in the docs). However, I just discovered that it is:
from bokeh.models import CategoricalColorMapper, Legend from bokeh.palettes import Category10 from bokeh.plotting import figure, show from bokeh.sampledata.iris import flowers color_mapper = CategoricalColorMapper( factors=[x for x in flowers['species'].unique()], palette=Category10) p = figure(height=350, width=500) p.add_layout(Legend(), 'right') p.circle("petal_length", "petal_width", source=flowers, legend_group='species', color=dict(field='species', transform=color_mapper)) show(p)
Apparently bokeh prefers plotting into existing legend objects, so by creating an empty one outside the figure, subsequent glyphs with the
legend_group parameter will fill in this legend object instead of creating a new one. At least that’s how I understand it.
Is this hack/feature considered stable? If so could I add a PR to the documentation with the sample above?