Skip to content

Instantly share code, notes, and snippets.

@rahul003
Created March 31, 2015 14:45
Show Gist options
  • Save rahul003/dbf29986cf8b52755b4f to your computer and use it in GitHub Desktop.
Save rahul003/dbf29986cf8b52755b4f to your computer and use it in GitHub Desktop.
Generate graph image from adjacency matrix
import networkx as nx
import matplotlib.pyplot as plt
import pylab
import csv
from random import random
G=nx.Graph()
with open('ai3.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
node = row[0]
i=1;
print node
while(True):
if(i>5 or row[2*i-1]==""):
break
G.add_edge(node,row[2*i-1], weight=row[2*i])
i+=1
pos=nx.circular_layout(G)
# version 2
pylab.figure(1,figsize=(40,40))
nx.draw_circular(G)
# specifiy edge labels explicitly
edge_labels=dict([((u,v,),d['weight'])
for u,v,d in G.edges(data=True)])
nx.draw_networkx_nodes(G,pos,node_size=1000)
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)
nx.draw_networkx_labels(G,pos,font_size=10,font_family='sans-serif')
# show graphs
pylab.show()
#plt.show()
plt.savefig("graph.png", dpi=1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment