Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


NetworkX Graph from CSV

NetworkX Graph from CSV

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 -->


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

CODE --> Python

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

data = StringIO.StringIO(""",,2,4,,3,4,,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)

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...

CODE --> 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)

First Graph (what i want)

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

Thanks for any help!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close