1

I am keeping track of multiple networkx graphs, that are modified X amount of iterations. Every time the graph changes, I recalculate all the positions using the spring layout function, providing the previous locations as input to keep the layouts more or less the same.

Unforunately, when some nodes get isolated, the distance between the main graph and the isolated node increases by a lot, which makes the main graph difficult to read. Is there a way to decrease the distance of the isolated nodes, so more space is available for the main graph? I have tried playing around with the K param, the optimal distance between nodes, but it hasn't yielded satisfactory results.

Here is an example:

import networkx as nx
import matplotlib.pyplot as plt

# Create a graph with initial positions
g = nx.random_geometric_graph(100, 0.125)
pos = nx.get_node_attributes(g, 'pos')

# Show the example graph's intial state
new_positions = nx.drawing.spring_layout(g, pos=pos, k=0.15)
nx.draw(g, new_positions, node_size=50)
plt.show()

# Remove the neighbors of the first 5 nodes to isolate them
for i in range(5):
    neighbors = list(g.neighbors(i))
    for nb in neighbors:
        g.remove_edge(i, nb)

# Draw the graph a 100 times using the spring layout algorithm
for i in range(100):
    new_positions = nx.drawing.spring_layout(g, pos=new_positions, k=0.15)

nx.draw(g, new_positions, node_size=50)
plt.show()

As can be seen in the image below, it is difficult to see the nodes in the center of the graph, because they are all squished together due to the isolated nodes around it. It would be nice if the space between isolated nodes could be targeted and reduced.

example_output

Tensza
  • 213
  • 1
  • 9
  • There are options for spring layout that control how many steps the spring-layout uses. That might control it. – Joel Nov 11 '20 at 04:59
  • When I run into this issue, I compute the layout of the components individually and then combine them using `rectangle-packer`. I have outlined these steps before in [this answer](https://stackoverflow.com/a/53156709/2912349) (second part, after the edit). – Paul Brodersen Feb 03 '21 at 13:01

0 Answers0