Skip to content

Instantly share code, notes, and snippets.

@jvfe
Created August 6, 2020 17:59
Show Gist options
  • Save jvfe/f20653088f39f51c98c2a5d35d5e80cd to your computer and use it in GitHub Desktop.
Save jvfe/f20653088f39f51c98c2a5d35d5e80cd to your computer and use it in GitHub Desktop.
Make a nice network plot with bokeh
from bokeh.io import show
from bokeh.models import Range1d, Plot, Circle, HoverTool, MultiLine
from bokeh.models.graphs import NodesAndLinkedEdges
from bokeh.plotting import from_networkx
import networkx as nx
def plot_network(network, tooltip, layout=nx.kamada_kawai_layout):
"""Makes a nice network plot with Bokeh
Mostly stuff I pieced together from the bokeh tutorials.
Args:
network: A networkx Graph or DiGraph object.
tooltip: A list of tuples that will define the tooltip. Follows this template:
[("Node", "@index"), ("Attr", "@attr")]
Where index corresponds the name of the node and attr is an attribute of the node.
layout: A networkx layout, for example kamada_kawai or spectral.
"""
plot = Plot(x_range=Range1d(-2, 2), y_range=Range1d(-2, 2))
# Create a Bokeh graph from the NetworkX input
graph = from_networkx(network, layout, scale=1.8, center=(0, 0))
plot.renderers.append(graph)
# Add some new columns to the node renderer data source
graph.node_renderer.glyph.update(size=20, fill_color="orange")
# When we hover over nodes, highlight nodes and adjacent edges
graph.node_renderer.hover_glyph = Circle(size=20, fill_color="#abdda4")
graph.edge_renderer.hover_glyph = MultiLine(line_color="#abdda4", line_width=4)
graph.inspection_policy = NodesAndLinkedEdges()
# Also add tooltip when hovering
hover_obj = HoverTool()
hover_obj.tooltips = tooltip
plot.add_tools(hover_obj)
show(plot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment