Skip to content

Instantly share code, notes, and snippets.

@canavandl
Created September 8, 2017 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save canavandl/8cb5ecece6ba720d09c0d1aef1666642 to your computer and use it in GitHub Desktop.
Save canavandl/8cb5ecece6ba720d09c0d1aef1666642 to your computer and use it in GitHub Desktop.
"""
Zachary's Karate Club graph
Data file from:
http://vlado.fmf.uni-lj.si/pub/networks/data/Ucinet/UciData.htm
Reference:
Zachary W. (1977).
An information flow model for conflict and fission in small groups.
Journal of Anthropological Research, 33, 452-473.
"""
import networkx as nx
from bokeh.io import show, curdoc
from bokeh.layouts import Row, Column
from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, TapTool, BoxSelectTool
from bokeh.models.graphs import from_networkx, NodesAndLinkedEdges, EdgesAndLinkedNodes
from bokeh.palettes import Spectral4
G=nx.karate_club_graph()
def create_graph(layout_func, inspection_policy=None, selection_policy=None, **kwargs):
plot = Plot(plot_width=400, plot_height=400,
x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
graph_renderer = from_networkx(G, layout_func, **kwargs)
graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Circle(size=15, fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Circle(size=15, fill_color=Spectral4[1])
graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=5)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=5)
# add meta data of start and end nodes to edge data source
graph_renderer.edge_renderer.data_source.data['metadata'] = G.edges()
graph_renderer.inspection_policy = inspection_policy
graph_renderer.selection_policy = selection_policy
plot.renderers.append(graph_renderer)
return plot
plot_3 = create_graph(nx.random_layout, inspection_policy=EdgesAndLinkedNodes(), scale=2, center=(0,0))
plot_3.title.text = "Random Layout (EdgesAndLinkedNodes inspection policy)"
plot_3.add_tools(HoverTool(line_policy='interp', tooltips=[("(Start, End)", "@metadata")]))
layout = plot_3
doc = curdoc()
doc.add_root(layout)
show(layout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment