Discover Communities: A Python Library for Graph Clustering Visualization

This article introduces the Python library communities, which detects and visualizes community structures in graph‑clustering problems, supports multiple algorithms such as Louvain and Girvan‑Newman, provides code examples for importing algorithms, building adjacency matrices, visualizing results with color coding, and creating animation GIFs of the clustering process.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Discover Communities: A Python Library for Graph Clustering Visualization

Recently a new Python visualization tool has become popular.

This tool, called communities , is a Python library for detecting and visualizing community structures in graph clustering problems.

Key Features

It supports multiple algorithms, including Louvain, Girvan‑Newman, hierarchical clustering, spectral clustering, and Bron‑Kerbosch, and can visualize the results.

Importing an algorithm and providing an adjacency matrix

Example using the Louvain algorithm. The algorithm works on an undirected graph represented by a 2‑D NumPy adjacency matrix.

import numpy as np
from communities.algorithms import louvain_method
adj_matrix = np.array([[0,1,1,0,0,0],
                      [1,0,1,0,0,0],
                      [1,1,0,1,0,0],
                      [0,0,1,0,1,1],
                      [0,0,0,1,0,1],
                      [0,0,0,1,1,0]])
communities, _ = louvain_method(adj_matrix)
# >>> [{0, 1, 2}, {3, 4, 5}]

After running the algorithm, the list of communities (each a set of node indices) is obtained.

Visualization with color coding

The draw_communities function visualizes the graph, assigns colors to each community, and allows options such as dark or light background, image resolution, and saving to file.

draw_communities(adj_matrix : numpy.ndarray,
                 communities : list,
                 dark : bool = False,
                 filename : str = None,
                 seed : int = 1)

Parameter meanings:

adj_matrix (numpy.ndarray): adjacency matrix of the graph.

dark (bool, default=False): dark background if True.

filename (str or None, default=None): path to save PNG; None shows interactively.

dpi (int or None, default=None): image resolution.

seed (int, default=2): random seed.

Animation of the algorithm

The louvain_animation function creates an animated GIF showing how nodes are assigned to communities over iterations.

louvain_animation(adj_matrix : numpy.ndarray,
                  frames : list,
                  dark : bool = False,
                  duration : int = 15,
                  filename : str = None,
                  dpi : int = None,
                  seed : int = 2)

Key arguments are similar to the visualization function, with frames containing per‑iteration community mappings and modularity values.

The library can also compute community adjacency matrices, Laplacian matrices, and modularity matrices.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Algorithmsvisualizationcommunity-detectionlouvaingraph-clustering
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.