How to increase node spacing for networkx.spring_layout
使用
绘制集团图
1 2 3 | import networkx as nx .... nx.draw(G, layout=nx.spring_layout(G)) |
产生以下图像:
显然,节点之间的间隔(例如,边缘长度)需要增加。我已经对此进行了搜索,并在这里找到了以下建议:
For some of the layout algorithms there is a
scale parameter that
might help. e.g.
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)
但是,
什么是获得更好图纸的正确方法?
此问题的解决方案在于尚未发布的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 |
调整这些参数以查看其工作原理。但是尽管如此,仍不能保证所有节点都不重叠。
您问题的真正答案是,原始图形不是单个完全连接的组件。它是三个单独的图。
正在发生的事情是三个部分飞到无穷远,这在重新缩放后使每个组件看起来像一个微小的斑点。
算法
因此,如果未连接图形,则由于没有任何连接,各个零件将飞离排斥力。有两个选项:更改力定律(编辑networkx代码),或分别绘制零件图。
这是增加力将所有节点吸引到图表中心的方法。将此代码段的最后一行添加到layouts.py:
中的
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)) |
单行代码使您可以制作如下图表:

而不是

但是,解决此问题的"最佳"方法是分别绘制组件图。
您可以遍历组件,并使用此处描述的功能将它们绘制在单独的图中。
有关更多讨论,请参见此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()) |