Long legends

Our legend has so many items in it that we cannot display them all without making the figure bigger just to accomodate the legend. (The legend is displayed to the right of the figure)

Is there some way we can display all the legend items without making the figure taller?

e.g.

  • make the legend scrollable
  • give the legend multiple tabs
  • split the legend into pages and make the pages selectable.

Currently using Bokeh 3.2.0

Thanks

Legends are not scrollable, since they are drawn directly onto the raster canvas of the plot using low-level drawing APIs. Eventually Legends may become separate DOM objects which may allow for scrolling, etc. In the mean time, your best bet is probably the nrows and ncols properties of legends to have the items arranged into grids, instead of just one single long list. There are some full examples here:

https://docs.bokeh.org/en/latest/docs/user_guide/styling/plots.html#two-dimensional-layout

With the just merged support for DOM legends (Add support for canvas and DOM rendering to `Legend` by mattpap · Pull Request #14028 · bokeh/bokeh · GitHub; bokeh 3.7.0.dev3) , it’s now quite easy to add at least basic/primitive support for scrollable legends like this:

import numpy as np

from bokeh.palettes import Spectral11
from bokeh.plotting import figure, show

p = figure(width=400, height=400)

y = 1
ys = np.array([1, 2, 3])

for color in Spectral11:
    p.scatter(x=[0, 1, 2], y=ys + y, size=[10, 15, 20], marker="circle", color=color, fill_alpha=0.5, legend_label=f"{color} item")
    y += 1

p.legend.location = "top_left"
p.legend.stylesheets.append("""
:host {
    max-height: 150px;
    overflow: scroll;
}
""")

show(p)

@quite68 please do note that the feature @mateusz describes above is not released yet. Bokeh 3.7 is probably a couple of months away from release.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.