Skip to content

Commit

Permalink
Hierarchical clustering layout gallery example (networkx#7058)
Browse files Browse the repository at this point in the history
* Add clustered layout gallery example.

* Minor viz tweaks.
  • Loading branch information
rossbar authored Oct 31, 2023
1 parent 948fdcd commit 5146f98
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/drawing/plot_clusters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
==============
Cluster Layout
==============
This example illustrates how to combine multiple layouts to visualize node
clusters.
The approach used here can be generalized to visualize hierarchical clustering
e.g. clusters-of-clusters of nodes by combining layouts with varying scale
factors.
"""
import networkx as nx
import matplotlib.pyplot as plt

G = nx.davis_southern_women_graph() # Example graph
communities = nx.community.greedy_modularity_communities(G)

# Compute positions for the node clusters as if they were themselves nodes in a
# supergraph using a larger scale factor
supergraph = nx.cycle_graph(len(communities))
superpos = nx.spring_layout(G, scale=50, seed=429)

# Use the "supernode" positions as the center of each node cluster
centers = list(superpos.values())
pos = {}
for center, comm in zip(centers, communities):
pos.update(nx.spring_layout(nx.subgraph(G, comm), center=center, seed=1430))

# Nodes colored by cluster
for nodes, clr in zip(communities, ("tab:blue", "tab:orange", "tab:green")):
nx.draw_networkx_nodes(G, pos=pos, nodelist=nodes, node_color=clr, node_size=100)
nx.draw_networkx_edges(G, pos=pos)

plt.tight_layout()
plt.show()

0 comments on commit 5146f98

Please sign in to comment.