Increase distance between nodes when using networkx.draw()

21,798 views
Skip to first unread message

arnegr

unread,
Feb 19, 2010, 7:33:36 AM2/19/10
to networkx-discuss
Hi,
i'd like to draw graphs with hundreds of nodes, but the nodes overlap
unless I choose a quite small size of the nodes. Is there any
possibilty to increase this distance or to not make them overlap?
Thanks in advance.
arne

Dan Schult

unread,
Feb 19, 2010, 10:42:27 AM2/19/10
to networkx...@googlegroups.com
Overlapping nodes can be solved by making the nodes smaller,
making the picture bigger or changing the layout. Changing
the layout sounds fine but is hard to do well.

It would be helpful to know what method you are using
to layout the nodes and which drawing method. If you
use the NetworkX defaults with matplotlib then you are using
the spring_layout routine. Take a look at the other
layout routines available.

NetworkX provides a few simple layout schemes, but it
isn't a drawing/layout package. pygraphviz gives an
interface to the GraphViz library, but you can also
output dot files from NetworkX that then can be processed
by GraphViz directly.
Dan

> --
> You received this message because you are subscribed to the Google
> Groups "networkx-discuss" group.
> To post to this group, send email to networkx-
> dis...@googlegroups.com.
> To unsubscribe from this group, send email to networkx-discuss
> +unsub...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/
> group/networkx-discuss?hl=en.
>

Aric Hagberg

unread,
Feb 19, 2010, 10:59:16 AM2/19/10
to networkx...@googlegroups.com
On Fri, Feb 19, 2010 at 5:33 AM, arnegr <arn...@gmx.de> wrote:
> Hi,
> i'd like to draw graphs with hundreds of nodes, but the nodes overlap
> unless I choose a quite small size of the nodes. Is there any
> possibilty to increase this distance or to not make them overlap?

For some of the layout algorithms there is a "scale" parameter that might
help. e.g.

In [1]: import networkx as nx

In [2]: G=nx.path_graph(4)

In [3]: pos=nx.spring_layout(G) #default to scale=1

In [4]: nx.draw(G,pos)

In [5]: pos=nx.spring_layout(G,scale=2) # double distance between all nodes

In [6]: nx.draw(G,pos)


Aric

arnegr

unread,
Feb 20, 2010, 1:34:00 PM2/20/10
to networkx-discuss
Hi,
thanks for your answers. I'm using the 'neato' layout, cause this is
the best for my purpose. If the scale-parameter is working with this
it would be great.
I gonna try later. arne

Michael

unread,
Mar 29, 2010, 8:51:08 PM3/29/10
to networkx-discuss
For those algorithms without a scale parameter, you can do it
manually. Just grab the coordinates of all of your nodes in a layout
output, and multiply them by a scale of your choice.

import networkx as nx
G = nx.generators.balanced_tree(3,3)
pos = nx.drawing.spring_layout(G)

scale = 1.25

#Written out long for clarity
for i in pos:
pos[i][0] = pos[i][0] * 2 # x coordinate
pos[i][1] = pos[i][1] * 2 # y coordinate

nickschurch

unread,
Mar 30, 2010, 6:10:15 AM3/30/10
to networkx-discuss
This doesn't help when plotting text though. Sure the distance between
the nodes increases, but that just changes the axes scales and the
text size doesn't scale witht he axis scale - so it won't really help
space out a graph with overlapping node labels.

Nick

Aric Hagberg

unread,
Mar 30, 2010, 9:02:02 AM3/30/10
to networkx...@googlegroups.com
On Tue, Mar 30, 2010 at 4:10 AM, nickschurch <nicks...@googlemail.com> wrote:
> This doesn't help when plotting text though. Sure the distance between
> the nodes increases, but that just changes the axes scales and the
> text size doesn't scale witht he axis scale - so it won't really help
> space out a graph with overlapping node labels.
>
You can change the figure size or the font size if that does what you want.
e.g.
----------
import networkx as nx
import pylab

G=nx.Graph()
G.add_edge(20*'a',20*'b')
G.add_edge(20*'b',20*'c')
G.add_edge(20*'c',20*'d')
G.add_edge(20*'d',20*'a')
G.add_edge(20*'a',20*'c')
pos=nx.spring_layout(G)
#pylab.figure(1,figsize=(3,3))
pylab.figure(1,figsize=(12,12))
pylab.xlim(0,1)
pylab.ylim(0,1)
nx.draw(G,pos,font_size=8)
#nx.draw(G,pos,font_size=10)
pylab.show()
----------
Aric

Reply all
Reply to author
Forward
0 new messages