Skip to content

Instantly share code, notes, and snippets.

@satomacoto
Created September 6, 2012 06:03
Show Gist options
  • Save satomacoto/3651907 to your computer and use it in GitHub Desktop.
Save satomacoto/3651907 to your computer and use it in GitHub Desktop.
NetworkX
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
# Zachary's Karate Club graph
G = nx.karate_club_graph()
# to directed
G = G.to_directed()
# draw & show
nx.draw(G, pos=nx.spring_layout(G))
#plt.show()
# dumps to json
import networkx.readwrite.json_graph as jsg
s = jsg.dumps(G)
#G = jsg.loads(s)
print s
# to numpy matrix
A = nx.to_numpy_matrix(G)
np.savetxt("myfile.txt", A)
A = np.genfromtxt("myfile.txt")
print A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment