forked from davebshow/ipython-gremlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_graph.py
74 lines (66 loc) · 2.44 KB
/
draw_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import networkx as nx
def draw_simple_graph(graph, node_type_attr='type',
edge_label_attr='weight', show_edge_labels=True,
label_attrs=['label'], k=None):
"""
Utility function to draw a labeled, colored graph with Matplotlib.
:param graph: networkx.Graph
"""
lbls = labels(graph, label_attrs=label_attrs)
clrs = colors(graph, node_type_attr=node_type_attr)
pos = nx.spring_layout(graph, weight=None, k=k)
if show_edge_labels:
e_labels = edge_labels(graph, edge_label_attr=edge_label_attr)
else:
e_labels = {}
nx.draw_networkx(graph, pos=pos, node_color=clrs, arrows=False)
nx.draw_networkx_edge_labels(graph, pos=pos, edge_labels=e_labels)
nx.draw_networkx_labels(graph, pos=pos, labels=lbls)
def labels(graph, label_attrs=['label']):
"""
Utility function that aggreates node attributes as
labels for drawing graph in Ipython Notebook.
:param graph: networkx.Graph
:returns: Dict. Nodes as keys, labels as values.
"""
labels_dict = {}
for node, attrs in graph.nodes(data=True):
label = u''
for k, v in attrs.items():
if k in label_attrs:
try:
label += u'{0}: {1}\n'.format(k, v)
except:
label += u'{0}: {1}\n'.format(k, v).encode('utf-8')
labels_dict[node] = label
return labels_dict
def edge_labels(graph, edge_label_attr='weight'):
"""
Utility function that aggreates node attributes as
labels for drawing graph in Ipython Notebook.
:param graph: networkx.Graph
:returns: Dict. Nodes as keys, labels as values.
"""
labels_dict = {}
for i, j, attrs in graph.edges(data=True):
label = attrs.get(edge_label_attr, '')
labels_dict[(i, j)] = label
return labels_dict
def colors(graph, node_type_attr='type'):
"""
Utility function that generates colors for node
types for drawing graph in Ipython Notebook.
:param graph: networkx.Graph
:returns: Dict. Nodes as keys, colors as values.
"""
colors_dict = {}
colors = []
counter = 1
for node, attrs in graph.nodes(data=True):
if attrs[node_type_attr] not in colors_dict:
colors_dict[attrs[node_type_attr]] = float(counter)
colors.append(float(counter))
counter += 1
else:
colors.append(colors_dict[attrs[node_type_attr]])
return colors