1

I have a network of 103 communities. Community Number 9 has the biggest number of around 12,000 members. I'm wanting to draw just that because the whole graph has around 70k nodes and its too big.

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')

I am able to get this, but what I want to see is a graph showing what the inside of a community would look like. Being the biggest community, I thought community 9 would be an ideal example.

enter image description here

Mtrinidad
  • 157
  • 1
  • 11

1 Answers1

0

You can take the subgraph of nodes in community 9 in the following way:

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

S will be a NetworkX graph with only the nodes in community 9 and the edges between them. Unfortunately, NetworkX may still have trouble plotting a graph of 12,000 nodes. Alternatives such as Gephi may be better.

Johannes Wachs
  • 1,270
  • 11
  • 15
  • Thanks Johannes. I have an additional question, if its not much of a bother. How do I export the subgraph so I can use it on Gephi? I tried converting it to dataframes first but that was probably a bit clumsy. – Mtrinidad May 07 '20 at 14:18
  • Sure thing. You can write NetworkX graphs write to file extensions .gexf and .gml with nx.write_gexf(S,'mygraph.gexf') or nx.write_gml(S,'mygraph.gml'). Within Gephi you can open either of these file types via File/Open. – Johannes Wachs May 07 '20 at 14:30