in src/graph_community.py [0:0]
def draw_graph_community(nx_G, communities, comm_order=None, color_map='rainbow', color_list=[], seed=42):
"""
Draw network with nodes colored based on community groups
:param nx_G: networkX graph
:param communities: list, a list with nodes grouped by communities, e.g., [{0, 1, 3}, {2, 4, 5}]
:param comm_order: list, order of communities to map communities to colors
:param color_map: str, one of the existing color map in matplotlib.pyplot
:param color_list: list, a list of color names for specifying a color for a community, e.g.,
color_list = ['tab:blue', 'tab:red', 'tab:green', 'tab:purple', 'tab:orange', 'tab:cyan']
:param seed: int, random seed for networkX layout
:return: a draw of network
"""
if comm_order is None:
# re-order communities based on the sum of node index for consistent coloring:
# we may still have inconsistent coloring among results from different number of communities
sum_nodes = [sum(i) for i in communities]
comm_order = sorted(range(len(sum_nodes)), key=lambda k: sum_nodes[k])
communities = [communities[i] for i in comm_order]
class_map = {}
if len(color_list) == 0:
for cl in range(len(communities)):
for n in communities[cl]:
class_map.update({n: cl})
class_map = dict(sorted(class_map.items()))
pos = nx.spring_layout(nx_G, seed=seed)
nx.draw(nx_G, cmap=plt.get_cmap(color_map), pos=pos, node_color=list(class_map.values()), with_labels=True,
font_color='white', node_size=500, font_size=10)
else:
assert len(color_list) >= len(communities), \
"Number of colors in color_list is less than the number of communities!!"
for cl in range(len(communities)):
for n in communities[cl]:
class_map.update({n: color_list[cl]})
class_map = dict(sorted(class_map.items()))
pos = nx.spring_layout(nx_G, seed=seed)
nx.draw(nx_G, pos=pos, node_color=list(class_map.values()), with_labels=True,
font_color='white', node_size=500, font_size=10)