How to draw a communities network in a circular graph

Viewed 227

I have a big graph and I extract from it a community network with the Louvain algorithm. Now I would draw a representation of these communities in a circular graph like this:

enter image description here

How can I do that? Is there a tool or a library of whatever programming language that can do this? Thank you in advance.

1 Answers

For the graph layout, you have NetworkX's nx.circular_layout, which will arrange the nodes as depicted above. There are some aspects that you might want to customize differently from what I've done here though. For instance for the edge colour I've just chosen the first node of each edge, and set a colour for the exact partition of that node. You could also add the labels of the important nodes, i.e those with a high centrality degree for instance, and the same for the node size.

Here's an example using a random graph:

from community import community_louvain
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import networkx as nx

G = nx.barabasi_albert_graph(300, 2)

# compute the best partition
partition = community_louvain.best_partition(G)
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)

plt.figure(figsize=(12,8))
# draw the graph
pos = nx.circular_layout(G)
# color the nodes according to their partition
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=100,
                       cmap=cmap, node_color=list(partition.values()))
edge_colors = [partition[edge[0]] for edge in G.edges()]
nx.draw_networkx_edges(G, 
                       pos, 
                       alpha=0.5,
                      edge_color=edge_colors)
plt.box(False)

enter image description here

Related