Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 805
clstaudt Avatar asked Jan 11 '13 17:01

clstaudt


People also ask

How many nodes can NetworkX handle?

For NetworkX, a graph with more than 100K nodes may be too large. I'll demonstrate that it can handle a network with 187K nodes in this post, but the centrality calculations were prolonged. Luckily, there are some other packages available to help us with even larger graphs.

What does NX Spring_layout do?

Position nodes using Fruchterman-Reingold force-directed algorithm. The algorithm simulates a force-directed representation of the network treating edges as springs holding nodes close, while treating nodes as repelling objects, sometimes called an anti-gravity force.

What is Ebunch in NetworkX?

ebunch. An iteratable container of edge tuples like a list, iterator, or file. edge attribute. Edges can have arbitrary Python objects assigned as attributes by using keyword/value pairs when adding an edge assigning to the G.


2 Answers

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

like image 188
Vikram Avatar answered Sep 20 '22 07:09

Vikram


The real answer 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_layout specifies a repulsive force between all nodes (anti-gravity), and an attractive force between only connected nodes (the "springs").

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.

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_reingold in layouts.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 here rather than enter image description here However 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 issue for more discussion.

like image 32
OrangeSherbet Avatar answered Sep 22 '22 07:09

OrangeSherbet