☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

Legend in network graph

Community Support
madegomez
madegomez

Hi!

I am trying to assign a legend to a network in such a way that the information of the colors of nodes and arcs is established.

I’ve tried

import networkx as nx

from bokeh.io import output_file, show
from bokeh.models import CustomJSTransform, LabelSet
from bokeh.models import Legend, LegendItem
from bokeh.plotting import figure, from_networkx

G=nx.nx.barbell_graph(3,2)

p = figure(x_range=(-3,3), y_range=(-3,3))
p.grid.grid_line_color = None

r = from_networkx(G, nx.spring_layout, scale=3, center=(0,0))
r.node_renderer.glyph.size=15
r.edge_renderer.glyph.line_alpha=0.2

p.renderers.append(r)
legend = Legend(items=[
    LegendItem(label="orange", renderers=[r.node_renderer])])
p.add_layout(legend)
show(p)

Thanks

⌁   Link to post
Bryan
Bryan

The above code generates the picture below, which is exactly what I would expect:

Screen Shot 2021-02-12 at 12.48.52 PM

If you change the node fill color:

r.node_renderer.glyph.fill_color="red"

Then the legend also changes correspondingly:

Screen Shot 2021-02-12 at 12.50.11 PM

which is also exactly what I would expect. You will need to describe what you are trying to accomplish in much more detail.

⌁   Link to post
Han
Han

Hey, I’m trying to do something similar and when using the above code snippet, it’s not working quite right atm. For one the legend doesn’t include the colored glyph:

Second, when I drag (as you would with box select) any area on the graph, things go crazy and I get a kind of recursive zoom as shown in this pic:


The behaviour on firefox is different but still buggy.

I came to this post because I’m also running into the same issue on a network plot and this is a nice reproducible example.

⌁   Link to post
Dror_M89
Dror_M89

Hi, I’m wondering how I can assign an interactive legend to the network to to mute specific communities in the network - is this possible?

My current function:

## Visualization function

def visualize_network(GraphObject, meta_df, day, save_map=False):
    
    #Create a network graph object with spring layout
# https://networkx.github.io/documentation/networkx-1.9/reference/generated/networkx.drawing.layout.spring_layout.html
    network_graph = from_networkx(GraphObject, 
                                  nx.spring_layout, 
                                  scale=10, center=(0, 0))
    ## create the colors, add meta label:
    cpalette = [item for sublist in Category20.values() for item in sublist]

    for i,r in meta_df.iterrows():
        if i == 0:
            network_graph.node_renderer.data_source.data.setdefault('COLORS', []).append((cpalette[r.Community]))
            network_graph.node_renderer.data_source.data.setdefault('title', []).append(r.title)
            network_graph.node_renderer.data_source.data.setdefault('Entities', []).append((r.doc_))
            network_graph.node_renderer.data_source.data.setdefault('Community', []).append((r.Community))
            network_graph.node_renderer.data_source.data.setdefault('Source', []).append((r.source))
            network_graph.node_renderer.data_source.data.setdefault('Date', []).append((r.publication_date.strftime('%d/%m/%Y')))
            
        else:
            network_graph.node_renderer.data_source.data['COLORS'].append((cpalette[int(r.Community)]))
            network_graph.node_renderer.data_source.data['title'].append(r.title)
            network_graph.node_renderer.data_source.data['Entities'].append((r.doc_))
            network_graph.node_renderer.data_source.data['Community'].append((r.Community))
            network_graph.node_renderer.data_source.data['Source'].append((r.source))
            network_graph.node_renderer.data_source.data['Date'].append((r.publication_date.strftime('%d/%m/%Y')))
            
    #Establish which categories will appear when hovering over each node
    HOVER_TOOLTIPS = [("Community","@Community"),("Title", "@title"), ("entities","@Entities"),
                      ('Source','@Source'),('Date','@Date')]
    #Create a plot — set dimensions, toolbar, and title
    plot = figure(tooltips = HOVER_TOOLTIPS,
              tools="pan,wheel_zoom,save,reset", active_scroll='wheel_zoom',
            x_range=Range1d(-10.1, 10.1), y_range=Range1d(-10.1, 10.1), 
                  title=f"Coverage of {EVENT} - Day {day}",
                 width=1000, height=800)
    
        #Set node size and color
    network_graph.node_renderer.glyph = Circle(size=15, fill_color='COLORS')

    #Set edge opacity and width
    network_graph.edge_renderer.glyph = MultiLine(line_alpha=0.5, line_width=1)

    #Add network graph to the plot
    plot.renderers.append(network_graph)

    if save_map is True:
        save(plot, filename=f"{EVENT}_Day{day}.html")
    else:
        show_map(plot)
    
    return

⌁   Link to post

End of archived discussion · Replies are disabled