Python Networkx Spring布局:减少隔离节点';到主图的距离

Python Networkx Spring布局:减少隔离节点';到主图的距离,python,networkx,graph-visualization,Python,Networkx,Graph Visualization,我正在跟踪多个networkx图,这些图是经过X次迭代修改的。每次图形更改时,我都会使用spring布局函数重新计算所有位置,提供以前的位置作为输入,以保持布局大致相同 不幸的是,当一些节点被隔离时,主图和隔离节点之间的距离增加了很多,这使得主图难以读取。有没有办法缩短孤立节点的距离,以便为主图提供更多的空间?我尝试过使用K参数(节点之间的最佳距离),但没有得到令人满意的结果 以下是一个例子: import networkx as nx import matplotlib.pyplot as p

我正在跟踪多个networkx图,这些图是经过X次迭代修改的。每次图形更改时,我都会使用spring布局函数重新计算所有位置,提供以前的位置作为输入,以保持布局大致相同

不幸的是,当一些节点被隔离时,主图和隔离节点之间的距离增加了很多,这使得主图难以读取。有没有办法缩短孤立节点的距离,以便为主图提供更多的空间?我尝试过使用K参数(节点之间的最佳距离),但没有得到令人满意的结果

以下是一个例子:

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()
如下图所示,很难看到图中心的节点,因为它们都被挤压在一起,因为它们周围有孤立的节点。如果孤立节点之间的空间可以被定位并减少,那就太好了


spring布局有一些选项,可以控制spring布局使用的步骤数。当我遇到这个问题时,我会单独计算组件的布局,然后使用
矩形包装器将它们组合起来。我在之前(第二部分,编辑之后)中概述了这些步骤。spring布局有一些选项,可以控制spring布局使用多少步骤。当我遇到这个问题时,我会单独计算组件的布局,然后使用
矩形包装器将它们组合起来。我在前面(第二部分,编辑之后)中概述了这些步骤。