I have the following code to make a bokeh plot from NetworkX
p = figure(x_range=(-1.1, 1.1), y_range=(-1.1, 1.1))
p.grid.visible = False
p.axis.visible = False
graph_renderer = from_networkx(G, nx.spring_layout, random_state=11, center=(0, 0), scale=1, k=0.5)
color_map = factor_cmap('domain_cat', factors=factors, palette=Category10_6)
graph_renderer.node_renderer.glyph = Circle(radius=0.02, fill_color=color_map, line_color=None, fill_alpha=1)
graph_renderer.edge_renderer.glyph = MultiLine(line_color='lightgray', line_alpha=0.3, line_width=2)
p.renderers.append(graph_renderer)
p.add_tools(HoverTool(tooltips='@index', show_arrow=None))
show(p)
It works great. However, I have a categorical color map for my nodes. I would like to add a legend.
When using the plotting interface you can easily add a categorical legend by just entering the source column name (https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html#colors).
However, I can't figure out how to generate, even through the models interface, using Legend and LegendItem, a categorical legend.
I have tried variants of:
items = [LegendItem(label=factor, renderers=[graph_renderer.node_renderer]) for factor in factors]
legend = Legend(items=items)
p.add_layout(legend)
But this produces the following result, with an empty legend that's the correct height and console errors that read TypeError: v is undefined; can't access its "draw_legend" property
.