edge length plot

2,870 views
Skip to first unread message

Rohan Dixit

unread,
Jan 5, 2009, 9:15:03 PM1/5/09
to networkx...@googlegroups.com
Hello and happy new year!

I was wondering if there's a way to draw a networkx graph with edges of prespecified lengths?

My idea was (ideally) to use each edge weight to determine its length.

Thanks,
Rohan

Aric Hagberg

unread,
Jan 5, 2009, 11:38:12 PM1/5/09
to networkx...@googlegroups.com

NetworkX doesn't have very sophisticated graph layout algorithms
but you might be able to get what you want by using the Graphviz
layout algorithms.

Here is an example using NetworkX with PyGraphviz to position the
nodes using the "weight" keyword or the "len" keyword.
(see http://graphviz.org/doc/info/attrs.html)

You'll need the latest NetworkX code (from SVN or
http://networkx.lanl.gov/download/networkx/)


#!/usr/bin/env python

import networkx
G=networkx.Graph()
G.add_edge(1,2,1)
G.add_edge(2,3,4)
# assigns edge weight as graphviz "weight" attribute
pos=networkx.graphviz_layout(G)
networkx.draw_networkx(G,pos)

H=networkx.Graph(G)
# replace all the edge data with a dictionary of
# the single graphviz keyword "len" set to the weight
for (u,v,w) in G.edges(data=True):
H.add_edge(u,v,{'len':str(w)})
pos=networkx.graphviz_layout(H)
networkx.draw_networkx(G,pos)


Aric

Reply all
Reply to author
Forward
0 new messages