Networkx pygraphviz conversion

20 views
Skip to first unread message

Screami...@gmail.com

unread,
Jul 9, 2007, 7:21:13 AM7/9/07
to networkx-discuss
Is it possible to convert a graph from one format to another, and/or
is it possible to have node labels in NX graph?

Aric Hagberg

unread,
Jul 9, 2007, 10:16:16 AM7/9/07
to networkx...@googlegroups.com
On Mon, Jul 09, 2007 at 11:21:13AM -0000, Screami...@gmail.com wrote:
> Is it possible to convert a graph from one format to another, and/or
> is it possible to have node labels in NX graph?

Have a look at the example pygraphviz_simple.py
https://networkx.lanl.gov/browser/networkx/trunk/doc/examples/pygraphviz_simple.py

That shows how to convert between networkx and pygraphviz.
The easiest way to get node labels is to use the string label as the
node id if you can.

If that doesn't work you can use XGraph which will automatically
get node labels from pygraphviz and put them in the node_attr
dictionary.
e.g.
using tree.dot from the graphviz distribution

>>> import pygraphviz
>>> import networkx
>>> A=pygraphviz.AGraph("tree.dot")
>>> A
digraph g {
node [height=".1",
shape=record
];
node0 [label="<f0> |<f1> G|<f2> "];
node4 [label="<f0> |<f1> R|<f2> "];
node0:f2 -> node4:f1;

.......

node7 [label="<f0> |<f1> A|<f2> "];
node2:f0 -> node7:f1;
node6 [label="<f0> |<f1> Y|<f2> "];
node4:f2 -> node6:f1;
node5 [label="<f0> |<f1> H|<f2> "];
node4:f0 -> node5:f1;
}
>>> N=networkx.XGraph(A)
>>> print N.nodes()
['node8', 'node1', 'node0', 'node3', 'node2', 'node5', 'node4',
>>> 'node7', 'node6']
>>> print N.node_attr
{'node8': {'label': '<f0> |<f1> C|<f2> '}, 'node1': {'label': '<f0>
|<f1> E|<f2> '}, 'node0': {'label': '<f0> |<f1> G|<f2> '},
'node3': {'label': '<f0> |<f1> F|<f2> '}, 'node2': {'label': '<f0>
|<f1> B|<f2> '}, 'node5': {'label': '<f0> |<f1> H|<f2> '},
'node4': {'label': '<f0> |<f1> R|<f2> '}, 'node7': {'label': '<f0>
|<f1> A|<f2> '}, 'node6': {'label': '<f0> |<f1> Y|<f2> '}}

Aric

Reply all
Reply to author
Forward
0 new messages