8

After generating x/y layout coordinates for a graph in NetworkX, how do I export the graph, along with node positions, as part of the node definition using something like GraphML?

The layout algorithms don't seem to annotate the graph directly? Or do they?!

Agostino
  • 2,723
  • 9
  • 48
  • 65
psychemedia
  • 5,690
  • 7
  • 52
  • 84

1 Answers1

11

The layout algorithms don't set node attributes (but they should). Here is how to set the attributes:

In [1]: import networkx as nx

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

In [3]: pos=nx.spring_layout(G)

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

In [5]: G.node
Out[5]: 
{0: {'pos': array([ 0.,  0.])},
 1: {'pos': array([ 0.32267963,  0.03340727])},
 2: {'pos': array([ 0.67729057,  0.07011044])},
 3: {'pos': array([ 1.        ,  0.10350174])}}
Aric
  • 24,511
  • 5
  • 78
  • 77
  • 1
    Could you also show how to export these positions to graphml? I can't get it to work. You can answer me in [this question](http://stackoverflow.com/q/28910466). Thanks. – Agostino Mar 07 '15 at 02:39
  • "But they should". What has been the fate of this? Any improvements to NetworkX as a result of this afterthought? – FaCoffee Sep 15 '17 at 11:17
  • There have been many improvements to networkx. This suggestion hasn't been implemented as far as I know. – Aric Sep 15 '17 at 23:54