Easy drawing of graphs with lots of nodes

1,202 views
Skip to first unread message

Nathan Schepers

unread,
Jan 26, 2014, 8:03:20 PM1/26/14
to networkx...@googlegroups.com
I'm very new to this, and I'm curious about something.  When I draw graphs with a small number of nodes & edges like this :

import networkx as nx

foo = nx.DiGraph()
foo.add_edge('one','two')
foo.add_edge('two','three')
foo.add_edge('one','three')
foo.add_edge('three','two')

nx.draw(foo)

I get a fairly comprehensible drawing of the graph.  (See attachment 1).

On the other hand, when I have a large(r) number of nodes & edges like this :

import networkx as nx
foo = nx.Graph()

for i in range(0,500):
    foo.add_edge(i,i+1)
    
nx.draw(foo)

I end up (aside from an intermittend divide by zero problem) with all of the nodes on top of each other.  (See attachment 2)

What do I need to change to get more readable drawings of graphs with lots of nodes? 
attachment 1.png
attachment 2.png

Brian Keegan

unread,
Jan 26, 2014, 9:19:26 PM1/26/14
to networkx...@googlegroups.com
You'll want to use one of the layout algorithms (such as spring layout) included in networkx to set node positions and then draw the network after this step.

foo = nx.random_graphs.barabasi_albert_graph(100,1)
p = nx.spring_layout(foo,iterations=50)
nx.draw(foo,pos=p)

There are other tools such as pygraphviz that have some more advanced options, but graph layouts very much require aesthetic judgements. If you're interested in visualizing larger graphs, you may be better off writing your graph to a file like GEXF or GraphML, and using a dedicated tool like Gephi to read in the data and visualize it in an interactive setting.


--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To post to this group, send email to networkx...@googlegroups.com.
Visit this group at http://groups.google.com/group/networkx-discuss.
For more options, visit https://groups.google.com/groups/opt_out.

Daniel Schult

unread,
Jan 26, 2014, 10:16:40 PM1/26/14
to networkx...@googlegroups.com
The spring_layout is used as a default if you don't specify a layout.
So that is not the problem here.

I think you just got incredibly unlucky in trying your example. If you
use 499 nodes instead of 500 it works fine. 501 works fine. 500
somehow puts all the points at [0,0] so the drawing looks like a
single node with 500 superimposed labels on it.

Maybe the spring_layout routine can be tweaked to make this bug not
happen. Thanks for reporting it.

Most of the time you should get a reasonable (if really messy and
hairball-like) picture of the graph.
Dan

Nathan Schepers

unread,
Jan 28, 2014, 2:02:25 PM1/28/14
to networkx...@googlegroups.com
Interesting... I was seeing this same behaviour when doing text analysis though (building a graph of subsequent words in a document), and it looks like any large document is causing this problem.  The 1..500 was just an easy way to reproduce it.  I'll dig up some code and show you.  I have it all in a sagemath ipython notebook.

Will someone be logging that bug somewhere, or should I be reporting it?

n.

Dan Schult

unread,
Jan 28, 2014, 2:09:53 PM1/28/14
to networkx...@googlegroups.com
I've entered an issue at
https://github.com/networkx/networkx/issues/1052

Any info or examples can be posted there. Hopefully we can figure out what in spring_layout makes it set every node to the origin. Meanwhile there are other tools that are probably better for graph layout as Brian mentioned earlier in this thread.

Dan

Dan Schult

unread,
Jan 28, 2014, 4:23:25 PM1/28/14
to networkx...@googlegroups.com
Nathan,
Aric found and I confirmed that this bug is already fixed in the latest version of NetworkX.
Sorry for the confusion.
Dan



On Jan 28, 2014, at 2:02 PM, Nathan Schepers wrote:

Nathan Schepers

unread,
Jan 28, 2014, 4:59:04 PM1/28/14
to networkx...@googlegroups.com
Okay.  I found it when tinkering around on sagemath with their ipython notebooks.  I guess they need to update their libraries.  Sorry for the confusion.
Reply all
Reply to author
Forward
0 new messages