Python 如何增加 networkx.spring_layout 的节点间距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14283341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:58:27  来源:igfitidea点击:

How to increase node spacing for networkx.spring_layout

pythongraphnetworkxgraph-drawing

提问by clstaudt

Drawing a clique graph with

绘制一个集团图

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

produces the following picture:

产生以下图片:

enter image description here

在此处输入图片说明

Obviously, the spacing between the nodes (e.g., the edge length) needs to be increased. I've googled this and found this suggestionhere:

显然,节点之间的间距(例如边长)需要增加。我在谷歌上搜索了这个并在这里找到了这个建议

For some of the layout algorithms there is a scaleparameter that might help. e.g.

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参数可能会有所帮助。例如

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)

However, the scaleparameter does not seem to have any effect.

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

What is the right method to get a better drawing?

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

采纳答案by Vikram

Alright, my answer is too late for this question. But the solution to this problem lies in the NetworkX version 1.8 which is yet to be released, but is available via git hub.

好吧,对于这个问题,我的回答为时已晚。但是这个问题的解决方案在于尚未发布的 NetworkX 1.8 版,但可以通过 git hub 获得。

Do the following to increase the distance between nodes:

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

    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

Tweak with these parameters to see how it works. But despite this there is no guarantee that all nodes are non-overlapping

调整这些参数,看看它是如何工作的。但尽管如此,并不能保证所有节点都不重叠

回答by James Houghton

I used the optimal distance parameter of the Kamada Kawai layout, and set the distance between non-connected components to the maximum distance in the graph. There is probably a better way of munging the dictionaries, but this is pretty easy:

我使用了 Kamada Kawai 布局的最佳距离参数,并将非连接组件之间的距离设置为图中的最大距离。可能有更好的方法来修改字典,但这很容易:

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())

回答by OrangeSherbet

The realanswer to your question is that your original graph is not a single, fully-connected component. It is three separate graphs.

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

What is happening is the three pieces are flying away to infinity, which upon re-scaling makes each component look like a tiny blob.

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

The algorithm spring_layoutspecifies a repulsive force between allnodes (anti-gravity), and an attractive force between onlyconnected nodes (the "springs").

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

Thus, if the graph isn't connected, the individual pieces will fly away from the repulsive force since there is nothing connecting them. There are two options: Change the force law (edit the networkx code), or graph the components separately.

因此,如果图形没有连接,各个部分将远离排斥力,因为没有任何连接它们。有两个选项:更改力定律(编辑 networkx 代码),或单独绘制组件。

Here's how to add a force that attracts all nodes to the center of the chart. Add the last line of this code snippet to def _fruchterman_reingoldin layouts.py:

以下是如何添加将所有节点吸引到图表中心的力。此代码段的最后一行添加到def _fruchterman_reingoldlayouts.py

# 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))

This single line of code allows you to make charts like: enter image description hererather than enter image description hereHowever the "best" way to deal with this is to chart the components seperately.

这行代码允许您制作如下图表: 在此处输入图片说明而不是 在此处输入图片说明然而,处理此问题的“最佳”方法是分别绘制组件图表。

You can iterate over the components, charting them in seperate plots, with the functions described here.

您可以使用此处描述的函数迭代组件,将它们绘制在单独的图中。

See this github issuefor more discussion.

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