I don’t think you can with an out of the box Legend, until this either this becomes a thing → [FEATURE] GlyphLegendItem for arbitrary legend items · Issue #10776 · bokeh/bokeh · GitHub or you create separate glyphs for each size bin you create (definitely not ideal either). My hack for this has been to mimic a legend with a figure. Basically you create a glyph you want to show in the Legend, and have it point to certain fields denoting its formatting (size, color, marker etc), and make an input dict specing those along with a ‘Label’ field:
from bokeh.plotting import figure, save
from bokeh.models import Scatter, LabelSet, ColumnDataSource
from bokeh.layouts import column
from bokeh.palettes import Spectral5
import numpy as np
def genLegendFigure(input_dict,glyph):
assert 'Label' in input_dict.keys()
l = figure(x_range=(-3,10),y_range=(-1*len(input_dict['Label']),1))
src = ColumnDataSource(input_dict)
src.data['yleg'] = np.arange(len(input_dict['Label']))*-1
glyph.x = 0
glyph.y='yleg'
r = l.add_glyph(src,glyph)
lbls = LabelSet(x=0,y='yleg',text='Label',source=src,x_offset=25,y_offset=-10)
lbl_r = l.add_layout(lbls)
l.toolbar.logo = None
l.toolbar_location = None
l.xgrid.grid_line_color = None
l.ygrid.grid_line_color = None
l.yaxis.visible = False
l.xaxis.visible = False
return l
input_dict = {'size':[5,10,15,20,25],'Label':['This','is','getting','gradually','bigger']
,'color':Spectral5}
g = Scatter(size='size',fill_color='color') #the glyph
leg = genLegendFigure(input_dict,g)
leg.height = 300
leg.width = 150
save(leg,'dumlegend.html')
You’ll have to play with the figure sizing and label offset requirements etc to get it to jive with your figure/data. Not amazing but it has worked in a pinch for me when I need full manual control of the “legend”