Skip to content

Instantly share code, notes, and snippets.

@clayadavis
Last active May 29, 2019 20:47
Show Gist options
  • Save clayadavis/8983421 to your computer and use it in GitHub Desktop.
Save clayadavis/8983421 to your computer and use it in GitHub Desktop.
Convert networkx graph to d3 graph
nodes = [{'name': n, 'group': G.node[n]['question_id'], 'size': G.node[n]['count']} for n in G]
l = G.edges()
edges = [{'source': l.index(s), 'target': l.index(t), 'value': G[s][t]['weight']} for s,t in itertools.product(l, l) if s in G and t in G[s]]
json.dump({'nodes': nodes, 'links': edges}, open('filename.json', 'w'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment