Graph layout tips

632 views
Skip to first unread message

prologic

unread,
Mar 20, 2009, 11:26:35 AM3/20/09
to networkx-discuss
Hi all,

Given this graph: http://shortcircuit.net.au/~prologic/kdb.png

Can anyone suggest any tips for improving it's visual look ?
It's a bit cluttered and kind of unreadable.

Thanks in advance,

cheers
James

PS: This is the code that generates it:

def edges(x, e=None, v=None):
if not e:
e = set()
if not v:
v = set()
for c in x.components.copy():
if c not in v:
v.add(c)
#e.add((x.name, c.name))
e.add((x, c))
edges(c, e, v)
return e

def graph(x, filename=None, interactive=False):
"""Display a directed graph of the Component structure of x

@param x: A Component or Manager to graph
@type x: Component or Manager

@return: A directed graph representing x's Component sturcture.
@rtype: str
"""

try:
import networkx as nx
import matplotlib.pyplot as plt

G = nx.MultiDiGraph()
G.add_edges_from(edges(x))

if not filename:
filename = "%s.png" % x.name

nx.draw(G,
#node_size=[len(n) * 500 for n in G.nodes()],
labels=dict((n, n.name) for n in G.nodes()),
plt.savefig(filename)
if interactive:
plt.show()
except ImportError:
pass

def printer(d, x):
return "%s* %s" % (" " * d, x)

return "\n".join(walk(x, printer))

Aric Hagberg

unread,
Mar 20, 2009, 9:23:05 PM3/20/09
to networkx...@googlegroups.com
On Fri, Mar 20, 2009 at 9:26 AM, prologic <prol...@shortcircuit.net.au> wrote:
>
> Hi all,
>
> Given this graph: http://shortcircuit.net.au/~prologic/kdb.png
>
> Can anyone suggest any tips for improving it's visual look ?
> It's a bit cluttered and kind of unreadable.

The spring layout isn't doing a great job with this directed graph and
the hacky code for drawing the labels isn't helping either is it? And
the "arrows" are awfully ugly...

So until we get the Matplotlib drawing code for directed graphs improved
(fix those arrows) my recommendation is to use a graph drawing package
to make prettier drawing. Graphviz (graphviz.org) will probably do a
good job. You can output to a "dot" file that Graphviz can understand
with networkx.write_dot(graph,file).

Aric

James Mills

unread,
Mar 21, 2009, 12:00:28 AM3/21/09
to networkx...@googlegroups.com
The whole reason I wanted to use networkx
was so I didn't have to use graphviz! graphviz
apparently depends on most of Xorg just ro be
able to use it :( (And in a server environment this
is not ideal).

Do you have any better suggestions ? :)

cheers
James

--
-- "Problems are solved by method"

Aric Hagberg

unread,
Mar 21, 2009, 5:56:10 PM3/21/09
to networkx...@googlegroups.com
On Fri, Mar 20, 2009 at 10:00 PM, James Mills
<prol...@shortcircuit.net.au> wrote:
>
> The whole reason I wanted to use networkx
> was so I didn't have to use graphviz! graphviz
> apparently depends on most of Xorg just ro be
> able to use it :( (And in a server environment this
> is not ideal).
>
> Do you have any better suggestions ? :)

The problem is partly that NetworkX has no specific layout algorithms
for directed graphs. I mentioned Graphviz because the "dot" algorithm
would probably do decent job on that graph.

If you can't use Graphviz and can't find any other layout generator
that works well you can always position the nodes yourself or code an
algorithm for that. I'd be willing to help if someone wanted to code
up the dot algorithm:
www.graphviz.org/Documentation/TSE93.pdf

Aric

James Mills

unread,
Mar 21, 2009, 7:53:30 PM3/21/09
to networkx...@googlegroups.com
On Sun, Mar 22, 2009 at 7:56 AM, Aric Hagberg <aric.h...@gmail.com> wrote:
> The problem is partly that NetworkX has no specific layout algorithms
> for directed graphs.  I mentioned Graphviz because the "dot" algorithm
> would probably do decent job on that graph.
>
> If you can't use Graphviz and can't find any other layout generator
> that works well you can always position the nodes yourself or code an
> algorithm for that.  I'd be willing to help if someone wanted to code
> up the dot algorithm:
> www.graphviz.org/Documentation/TSE93.pdf

I might be willing to assist with this also, as it's actually quite a
pain to have GraphViz and all it's required dependencies installed
on a server - even though I do use it for other things to *sigh*

networkx + matplotlib (with no other deps) is nicer though and
more lightweight.

cheers
James

Richard Nowakowski

unread,
Mar 23, 2009, 8:50:17 AM3/23/09
to networkx...@googlegroups.com
I usually use Networkx to set up my network and analyze it. To make it
pretty I export the graph to pydot
(e.g., Gpydot = nx.to_pydot(G). With pydot I can format the graph quite
nicely, using neato, dot or fdp. Pydot is a python wrapper for graphviz, so
it might not help you; however, visually the results are nice and quite
flexible.

Richard
Reply all
Reply to author
Forward
0 new messages