Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NetworkX Graph from CSV

Status
Not open for further replies.

topcat01

Programmer
Jul 10, 2003
83
GB
I have two working scripts, but neither of them as I would like. The sample data file I have is in a file called 'file2.txt'

Code:
Email,IP,weight,att1
jim.bob@junk.com,192.168.0.1,2,4
steve.bob@mc.com,192.168.0.1,3,4
mary.bob@video.com,192.168.0.25,4,4

The first script I have works and produces a graph I want, however the input comes from StringIO (i want from csv in chunks)

Python:
import StringIO
import networkx as nx
import matplotlib.pyplot as plt


data = StringIO.StringIO("""
jim.bob@junk.com,192.168.0.1,2,4
steve.bob@mc.com,192.168.0.1,3,4
mary.bob@video.com,192.168.0.25,4,4
""")


G = nx.read_edgelist(data, delimiter=',', nodetype=str, data=[('weight',int),('att1',int)])
for e in G.edges():
    print e

pos = nx.spring_layout(G,scale=1)

node_labels = nx.get_node_attributes(G,'state')
nx.draw_networkx_labels(G, pos, labels = node_labels)
edge_labels = nx.get_edge_attributes(G,'state')
nx.draw_networkx_edge_labels(G, pos, labels = edge_labels)

nx.draw(G,pos, with_labels=True)
plt.savefig('this.png')
plt.show()

To try and read the data from a large data file i want to read in chunks, so got to the following point which is in the right direction but the output graph is wrong...

Python:
import matplotlib.pyplot as plt
import pandas as pd
import networkx as nx

G = nx.DiGraph()

for d in pd.read_csv("file2.txt",sep='\s+', header=None, names=['A', 'B'], chunksize=10000):
    G.add_edges_from([tuple(x) for x in d.values])

for e in G.edges():
    print e

pos = nx.spring_layout(G,scale=1)
nx.draw(G,pos, with_labels=True)
plt.savefig('this.png')
plt.show()

First Graph (what i want)
this_bdjesm.png


Second Graph (dont understand why it is like this...)
this_bqduga.png


Thanks for any help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top