如何增加 networkx.spring_layout 的节点间距 [英] How to increase node spacing for networkx.spring_layout

查看:36
本文介绍了如何增加 networkx.spring_layout 的节点间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

产生以下图片:

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

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

对于一些布局算法,有一个 scale 参数可能有帮助.例如

For some of the layout algorithms there is a scale parameter 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 参数似乎没有任何效果.

However, the scale parameter does not seem to have any effect.

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

What is the right method to get a better drawing?

推荐答案

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

The solution to this problem lies in the NetworkX version 1.8 which is yet to be released, but is available via 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

这篇关于如何增加 networkx.spring_layout 的节点间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆