Skip to content

Instantly share code, notes, and snippets.

@sergeyk
Created April 17, 2013 18: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 sergeyk/5406649 to your computer and use it in GitHub Desktop.
Save sergeyk/5406649 to your computer and use it in GitHub Desktop.
plotting ilsvrc65
# assumes there is a dictionary that has keys
# 'g': the nx.DiGraph graph
# 'nodes': list of node names
# 'heights': list of integer heights of the nodes
fig = plt.figure()
ax = fig.add_subplot(111)
g = graph['g']
nodes = graph['nodes']
pos = nx.pygraphviz_layout(g, prog='dot')
gray = (0.75, 0.75, 0.75)
graydark = (0.5, 0.5, 0.5)
ns = 120
labels = dict(zip(nodes, [g.node[node]['word'].split(',')[0] for node in nodes]))
leaf_nodes = np.array(graph['nodes'])[graph['heights'] == 0]
for node in leaf_nodes:
labels[node] = ''
cmap = plt.get_cmap('Oranges')
for h in np.unique(graph['heights']):
h_nodes = np.array(graph['nodes'])[graph['heights'] == h].tolist()
c = 1 # can change this to shade nodes according to some measurement
nx.draw_networkx_nodes(g, pos, nodelist=h_nodes, node_size=ns, node_color=cmap(c), ax=ax).set_edgecolor(graydark)
if h > 0:
pos_offset = pos.copy()
for k in pos_offset.keys():
pos_offset[k] = (pos_offset[k][0], pos_offset[k][1]-20)
nx.draw_networkx_labels(g, pos_offset, labels, font_size=12, font_color=graydark)
nx.draw_networkx_edges(g, pos, arrows=False, edge_color=[gray] * len(g.edges()), ax=ax)
plt.axis('off')
plt.axis('equal')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment