Playing with graphs


In [3]:
import networkx as nx
import matplotlib.pyplot as plt

keywordTreeFile = open('decode_wordnet/keywordTreeFile.TXT','r') #keyword generation file
keywordTreeFileLineData = keywordTreeFile.readlines()
#keywordTreeFileLineData[4].split('\t')[1].rstrip('\n')

G = nx.Graph()
pairData = []

for i in range(0,len(keywordTreeFileLineData)):
    pairData = pairData + [keywordTreeFileLineData[i].split('\t')]
    pairData[i][1] = pairData[i][1].rstrip('\n')
    G.add_edge(pairData[i][0],pairData[i][1])

#pos = nx.spring_layout(G,k=.15,iterations=50,scale=100)
pos = nx.graphviz_layout(G)
#labels = nx.draw_networkx_labels(G,pos)
nx.draw(G,
        pos=pos,
        alpha=.4,
        #node_color = np.linspace(0,1,len(G.nodes())),
        #edge_color = np.linspace(0,1,len(G.edges())),
        edge_color = 'green',
        node_size = 100,
        width = 1.0,
        with_labels = True,
        font_size = 2,
        linewidths=.1
        )

plt.savefig("graph.pdf")



In [6]:
nx.draw_networkx?

In [2]:
import sys
print sys.prefix


/Users/scott/Library/Enthought/Canopy_64bit/User

In [7]:
import community
import networkx as nx
import matplotlib.pyplot as plt

#better with karate_graph() as defined in networkx example.
#erdos renyi don't have true community structure
#G = nx.erdos_renyi_graph(30, 0.05)
G=nx.karate_club_graph()
#first compute the best partition
partition = community.best_partition(G)

#drawing
size = float(len(set(partition.values())))
pos = nx.graphviz_layout(G)
count = 0.
for com in set(partition.values()) :
    count = count + 1.
    list_nodes = [nodes for nodes in partition.keys()
                                if partition[nodes] == com]
    nx.draw_networkx_nodes(G, pos, list_nodes, node_size = 20,
                                node_color = str(count / size))


nx.draw_networkx_edges(G,pos, alpha=0.5, with_labels = True)
plt.show()



In [ ]: