检索绘制的NetworkX图节点的坐标

时间:2018-01-02 02:27:05

标签: python networkx

在下面的代码中,我在2D中绘制邻接矩阵,如下图所示。由于networkx能够绘制节点,因此必须有节点坐标的信息,我想知道是否有任何方法可以检索绘制图形中每个节点的二维坐标。谷歌搜索和查看文档我没有任何运气。

import networkx as nx
from networkx.drawing.nx_agraph import to_agraph

dt = [('len', float)]
A = np.array([(0, 5, 5, 5),
           (0.3, 0, 0.9, 0.2),
           (0.4, 0.9, 0, 0.1),
           (1, 2, 3, 0)
           ])*10

A = A.view(dt)
G = nx.from_numpy_matrix(A)

G.draw('distances_1.png', format='png', prog='neato')

enter image description here

1 个答案:

答案 0 :(得分:2)

您的代码未按书面形式运行。但我认为你只需要通过调用graphviz_layout(使用pygraphviz)获得neato布局位置。 结果是一个字典,其中节点为键,位置为值。

In [1]: import networkx as nx

In [2]: import numpy as np

In [3]: from networkx.drawing.nx_agraph import graphviz_layout

In [4]: A = np.array([(0, 5, 5, 5),
   ...:            (0.3, 0, 0.9, 0.2),
   ...:            (0.4, 0.9, 0, 0.1),
   ...:            (1, 2, 3, 0)
   ...:            ])*10

In [5]: G = nx.from_numpy_matrix(A)

In [6]: pos = graphviz_layout(G, prog='neato')

In [7]: pos
Out[7]: 
{0: (-42.946, -6.3677),
 1: (42.991, 6.3533),
 2: (6.3457, -42.999),
 3: (-6.3906, 43.014)}