Drawing nodes that don't overlap

7,407 views
Skip to first unread message

ses1984

unread,
Jun 30, 2011, 3:30:54 PM6/30/11
to networkx-discuss
I'm trying to draw a graph like this and, after poring over the docs
for a couple of hours, I haven't been able to figure out how to space
out the nodes so they aren't on top of each other.

http://i.imgur.com/5jxLb.png

Thanks.

ses1984

unread,
Jun 30, 2011, 3:33:13 PM6/30/11
to networkx-discuss
I am sorry that I forgot to include that I am drawing my graph like
this:

nx.draw(g, nx.graphviz_layout(g, prog='dot'))

where the variable 'g' represents the graph.

Aric Hagberg

unread,
Jun 30, 2011, 4:11:52 PM6/30/11
to networkx...@googlegroups.com

If you want to stay with the same layout (not sure which one you are using),
you can adjust the figure size, the node size and the label size to
try to get what you want.

Try this for example to see how to change things

import pylab
import networkx as nx
G=nx.cycle_graph(80)
pos=nx.circular_layout(G)
pylab.figure(1)
nx.draw(G,pos)
pylab.figure(2)
nx.draw(G,pos,node_size=60,font_size=8)
pylab.figure(3,figsize=(12,12))
nx.draw(G,pos)
pylab.show()


Aric

ses1984

unread,
Jun 30, 2011, 4:34:15 PM6/30/11
to networkx-discuss
On Jun 30, 4:11 pm, Aric Hagberg <ahagb...@gmail.com> wrote:
> If you want to stay with the same layout (not sure which one you are using),
> you can adjust the figure size, the node size and the label size to
> try to get what you want.

It seems like I could easily tune these parameters by trial and error,
but I don't see how this solution would work for lots of arbitrarily
different graphs.

All of my graphs are trees. I'm not specifying a layout of any kind
except for here: nx.draw(g, nx.graphviz_layout(g, prog='dot'))

Aric Hagberg

unread,
Jun 30, 2011, 4:39:55 PM6/30/11
to networkx...@googlegroups.com
On Thu, Jun 30, 2011 at 2:34 PM, ses1984 <ses...@gmail.com> wrote:

Graph drawing isn't easy, and NetworkX isn't primarily a graph drawing
package...
But since you are using graphviz for the layout you might consider
using the graphviz rendering too.
You can
>>> nx.write_dot(g,'file.dot')
and then process the resulting file however you like with graphviz.

You can do that directly from Python with either
P = nx.to_pydot(G) #pydot
or
A = nx.to_agraph(G) #pygraphviz

And then use those programs to create layout and images.

Aric

Reply all
Reply to author
Forward
0 new messages