关于python:如何增加networkx.spring_layout的节点间距

How to increase node spacing for networkx.spring_layout

使用

绘制集团图

1
2
3
import networkx as nx
....
nx.draw(G, layout=nx.spring_layout(G))

产生以下图像:

enter

1
2
3
4
5
6
import networkx as nx
G = nx.path_graph(4)
pos = nx.spring_layout(G)  # default to scale=1
nx.draw(G, pos)
pos = nx.spring_layout(G, scale=2)  # double distance between all nodes
nx.draw(G, pos)

但是,scale参数似乎没有任何作用。

什么是获得更好图纸的正确方法?


此问题的解决方案在于尚未发布的NetworkX 1.8版,但可以通过git hub获得。

执行以下操作以增加节点之间的距离:

1
2
3
4
pos = nx.spring_layout(G, k=0.15, iterations=20)
# k controls the distance between the nodes and varies between 0 and 1
# iterations is the number of times simulated annealing is run
# default k=0.1 and iterations=50

调整这些参数以查看其工作原理。但是尽管如此,仍不能保证所有节点都不重叠。


您问题的真正答案是,原始图形不是单个完全连接的组件。它是三个单独的图。

正在发生的事情是三个部分飞到无穷远,这在重新缩放后使每个组件看起来像一个微小的斑点。

算法spring_layout指定所有节点之间的排斥力(反重力),以及仅连接的节点之间的吸引力("弹簧")。

因此,如果未连接图形,则由于没有任何连接,各个零件将飞离排斥力。有两个选项:更改力定律(编辑networkx代码),或分别绘制零件图。

这是增加力将所有节点吸引到图表中心的方法。将此代码段的最后一行添加到layouts.py:

中的def _fruchterman_reingold

1
2
3
4
5
6
# displacement"force"
displacement = np.einsum('ijk,ij->ik',
                         delta,
                         (k * k / distance**2 - A * distance / k))
# ADD THIS LINE - prevent things from flying off into infinity if not connected
displacement = displacement - pos / ( k * np.sqrt(nnodes))

单行代码使您可以制作如下图表:
enter

您可以遍历组件,并使用此处描述的功能将它们绘制在单独的图中。

有关更多讨论,请参见此github问题。


我使用了Kamada Kawai布局的最佳距离参数,并将未连接的组件之间的距离设置为图中的最大距离。也许有更好的方法来修饰词典,但这很简单:

1
2
3
4
5
6
7
8
df = pd.DataFrame(index=G.nodes(), columns=G.nodes())
for row, data in nx.shortest_path_length(G):
    for col, dist in data.items():
        df.loc[row,col] = dist

df = df.fillna(df.max().max())

layout = nx.kamada_kawai_layout(G, dist=df.to_dict())