Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

python - How to increase node spacing for networkx.spring_layout

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 suggestion here:

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)

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

What is the right method to get a better drawing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...