Skip to content

Instantly share code, notes, and snippets.

@canavandl
Created July 27, 2017 14:46
Show Gist options
  • Save canavandl/3bcc12e63d31144cc500ef707916cea7 to your computer and use it in GitHub Desktop.
Save canavandl/3bcc12e63d31144cc500ef707916cea7 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, Title, Range1d, MultiLine, Circle, Rect, Ellipse, Text
from bokeh.palettes import Spectral4
from bokeh.models.graphs import from_networkx, NodesAndLinkedEdges
from bokeh.models.tools import HoverTool, TapTool, BoxSelectTool
PLOT_OPTS = dict(
toolbar_location="right",
plot_width=400,
plot_height=400,
x_range=Range1d(-1.1,1.1),
y_range=Range1d(-1.1,1.1),
)
EDGE_OPTS = dict(
line_color="#CCCCCC",
line_alpha=0.8
)
G=nx.karate_club_graph()
p1 = Plot(title=Title(text="Circular Layout (NodeOnly inspection policy)"), **PLOT_OPTS)
gr1 = from_networkx(G, nx.circular_layout, scale=1, center=(0,0))
gr1.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0])
gr1.edge_renderer.glyph = MultiLine(**EDGE_OPTS)
p1.renderers.append(gr1)
p1.add_tools(HoverTool())
p2 = Plot(title=Title(text="Random Layout (NodeOnly selection policy)"), **PLOT_OPTS)
gr2 = from_networkx(G, nx.random_layout, scale=2, center=(0,0))
gr2.node_renderer.glyph = Rect(width=0.05, height=0.05, fill_color=Spectral4[1])
gr2.edge_renderer.glyph = MultiLine(**EDGE_OPTS)
p2.renderers.append(gr2)
p2.add_tools(TapTool(), BoxSelectTool())
p3 = Plot(title=Title(text="FR Layout (NodesAndLinkedEdges inspection policy)"), **PLOT_OPTS)
gr3 = from_networkx(G, nx.fruchterman_reingold_layout, scale=2, center=(0,0), dim=2)
gr3.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[2])
gr3.edge_renderer.glyph = MultiLine(**EDGE_OPTS)
gr3.node_renderer.hover_glyph = Circle(size=15, fill_color='teal')
gr3.edge_renderer.hover_glyph = MultiLine(line_color='red', line_alpha=1.0)
gr3.inspection_policy = NodesAndLinkedEdges()
p3.renderers.append(gr3)
p3.add_tools(HoverTool(tooltips=None))
p4 = Plot(title=Title(text="Spring Layout (NodesAndLinkedEdges selection policy)"), **PLOT_OPTS)
gr4 = from_networkx(G, nx.spring_layout, scale=2, center=(0,0))
gr4.node_renderer.glyph = Rect(width=0.05, height=0.05, fill_color=Spectral4[3])
gr4.edge_renderer.glyph = MultiLine(**EDGE_OPTS)
gr4.node_renderer.selection_glyph = Rect(fill_color='purple')
gr4.edge_renderer.selection_glyph = MultiLine(line_color='red')
gr4.selection_policy = NodesAndLinkedEdges()
p4.renderers.append(gr4)
p4.add_tools(TapTool(), BoxSelectTool())
layout = Column(Row(p1, p2), Row(p3, p4))
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