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

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

问题描述

使用

 >将networkx导入为nx 
....
nx。 draw(G,layout = nx.spring_layout(G))

产生下图:





显然,节点之间的间隔(例如边缘长度)需要增加。我已使用Google搜索并找到此建议这里:


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



1 中:导入networkx为nx



2 :G = nx.path_graph(4)

在[3]中:pos = nx.spring_layout (G)#default to scale = 1



在[4]中:nx.draw(G,pos)

<在[5]中:pos = nx.spring_layout(G,scale = 2)#所有
节点之间的双倍距离

在[6]中:nx .draw(G,pos)

但是,比例参数不会似乎有任何影响。



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

解决方案

好的,我的回答对于这个问题来说太晚了。但是这个问题的解决方案在于尚未发布的NetworkX 1.8版本,但可通过git hub获得。



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

  pos = nx.spring_layout(G,k = 0.15,迭代= 20)
#k控制节点之间的距离并且在0和1之间变化
#迭代次数是模拟退火运行的次数
#default k = 0.1和迭代= 50

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

Drawing a clique graph with

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

produces the following picture:

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

For some of the layout algorithms there is a "scale" parameter that might help. e.g.

In 1: import networkx as nx

In 2: G=nx.path_graph(4)

In [3]: pos=nx.spring_layout(G) #default to scale=1

In [4]: nx.draw(G,pos)

In [5]: pos=nx.spring_layout(G,scale=2) # double distance between all nodes

In [6]: nx.draw(G,pos)

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

What is the right method to get a better drawing?

解决方案

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.

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天全站免登陆