Exporting Layout Positions for a Graph Using NetworkX

前端 未结 1 821
小鲜肉
小鲜肉 2020-12-18 01:48

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 Gr

相关标签:
1条回答
  • 2020-12-18 01:54

    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])}}
    
    0 讨论(0)
提交回复
热议问题