我有一个由 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')