如何在忽略其他社区的情况下从 networkx 中提取特定社区?

How to draw specific communities from networkx while ignoring the other communities?

我有一个包含 103 个社区的网络。 9 号社区拥有最多的成员,大约有 12,000 名成员。我想画那个是因为整个图有大约 70k 个节点而且它太大了。

largest_subgraph = max(nx.connected_component_subgraphs(graph),key=len)

partition=community.best_partition(largest_subgraph)
values=[partition.get(node) for node in largest_subgraph.nodes()]
list_com=partition.values()

dict_nodes={}

for each_item in partition.items():
    community_num=each_item[1]
    community_node=each_item[0]
    if community_num in dict_nodes:
        value=dict_nodes.get(community_num) + ' | ' + str(community_node)
        dict_nodes.update({community_num:value})
    else:
        dict_nodes.update({community_num:community_node})

plt.rcParams['figure.figsize']= [12, 8]
G_comm=nx.Graph()

G_comm.add_nodes_from(dict_nodes)

mod=community.modularity(partition,largest_subgraph)

plt.rcParams['figure.figsize']= [12, 8]
pos_louvain=nx.spring_layout(G_comm)
nx.draw_networkx(G_comm, pos_louvain, with_labels=True,node_size=200,font_size=11,label='Modularity =' + str(round(mod,3)) +
                    ', Communities=' + str(len(G_comm.nodes())))
plt.suptitle('Number of Communities(Louvain Algorithm)',fontsize=22,fontname='Arial')
plt.box(on=None)
plt.axis('off')
plt.legend(bbox_to_anchor=(1,0), loc='best', ncol=1)
plt.savefig('louvain1.png',dpi=400, bbox_inches='tight')

我能得到这个,但我想看到的是一张显示社区内部情况的图表。作为最大的社区,我认为社区 9 将是一个理想的例子。

社区9的节点subgraph可以通过以下方式获取:

nodes_in_community9 = [node for node,community in partition.items() if community == 9]
S = G.subgraph(nodes_in_community9)

S 将是一个 NetworkX 图,只有社区 9 中的节点和它们之间的边。不幸的是,NetworkX 可能仍然无法绘制包含 12,000 个节点的图形。 Gephi 之类的替代品可能更好。