Using legends with Circle

Hi everyone, I’ve found a lot online about using legends with circle() but not Circle()

I am currently plotting two different data sources like so:

boda_source = ColumnDataSource(dict(boda_id = locations["boda"]["id"],
                               boda_x = locations["boda"]["x"],
                               boda_y = locations["boda"]["y"],
                               boda_lon = locations["boda"]["longitude"],
                               boda_lat = locations["boda"]["latitude"]

static_source = ColumnDataSource(dict(static_id = locations["static"]["id"],
                               static_x = locations["static"]["x"],
                               static_y = locations["static"]["y"],
                               static_lon = locations["static"]["longitude"],
                               static_lat = locations["static"]["latitude"]))

# Hover tools
boda_hover = HoverTool(names=["boda"],
                       tooltips = [("Type", "Mobile"),
                                   ("Sensor ID", "@boda_id"),
                                   ("Lat/long", "@boda_lat, @boda_lon")])

static_hover = HoverTool(names=["static"],
                         tooltips = [("Type", "Static"),
                                     ("Sensor ID", "@static_id"),
                                     ("Lat/long", "@static_lat, @static_lon")])
# Plot figure and add tiles 
mapv2 = figure(title=None, 
                    x_range=(mercx0[0], mercxend[0]), 
                    y_range=(mercy0[0], mercyend[0]),
                    tools = ["pan,wheel_zoom,box_zoom,reset", boda_hover, static_hover])


# Create and plot glyphs for predictions
boda_glyph = Circle(x="boda_x", y="boda_y", size= 10, line_color="white", fill_color="green", fill_alpha=0.8, line_width=1, 
static_glyph = Circle(x="static_x", y="static_y", size= 10, line_color="white", fill_color="red", fill_alpha=0.8, line_width=1, 

mapv2.add_glyph(boda_source, boda_glyph, name="boda")
mapv2.add_glyph(static_source, static_glyph, name="static")

But I can’t work out how to get legends to work with the glyphs if I’m not just adding markers directly.

Hi @Dennis_M_Reddyhoff

The last two lines of your code generate renderers for the boda and static glyphs, respectively. If you assign these to variables you will have access to the renderers with which you can add legends. As an illustration try something like the following … (note the additional imports req’d)

from bokeh.models import ColumnDataSource, HoverTool, LegendItem, Legend

r_boda = mapv2.add_glyph(boda_source, boda_glyph, name="boda")
r_static = mapv2.add_glyph(static_source, static_glyph, name="static")

li = []
li += [LegendItem(label='boda', renderers=[r_boda])]
li += [LegendItem(label='static', renderers=[r_static])]
_legend = Legend(items = li)



Thank you! I was trying to work out what my renderers were in this situation