Dendrogram
Soft Glow Clusters Light
Light theme with soft glowing cluster regions
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage, fcluster, set_link_color_palette
from matplotlib.patches import FancyBboxPatch
np.random.seed(789)
labels = ['Node_' + str(i) for i in range(1, 13)]
data = np.random.rand(len(labels), 5) * 60
Z = linkage(data, method='complete')
fig, ax = plt.subplots(figsize=(14, 8), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
set_link_color_palette(['#F5276C', '#27D3F5', '#6CF527', '#F5B027'])
dn = dendrogram(Z, labels=labels, leaf_rotation=0, leaf_font_size=11,
color_threshold=0.5*max(Z[:,2]), above_threshold_color='#9ca3af', ax=ax)
# Soft cluster boxes
clusters = fcluster(Z, t=4, criterion='maxclust')
glow_colors = [('#F5276C', '#F5276C15'), ('#27D3F5', '#27D3F515'),
('#6CF527', '#6CF52715'), ('#F5B027', '#F5B02715')]
ivl = dn['ivl']
leaves = dn['leaves']
cluster_ranges = {}
for i, leaf in enumerate(leaves):
c = clusters[leaf]
if c not in cluster_ranges:
cluster_ranges[c] = [i, i]
else:
cluster_ranges[c][1] = i
for c, (start, end) in cluster_ranges.items():
edge_color, fill_color = glow_colors[(c-1) % len(glow_colors)]
rect = FancyBboxPatch((start*10 - 5, 0), (end - start + 1)*10, max(Z[:,2])*0.9,
boxstyle='round,pad=0.02', facecolor=fill_color,
edgecolor=edge_color, linewidth=1.5, zorder=0)
ax.add_patch(rect)
ax.set_title('Network Clustering Analysis', fontsize=15,
color='#1f2937', fontweight='bold', pad=20)
ax.set_xlabel('Nodes', fontsize=11, color='#374151')
ax.set_ylabel('Distance', fontsize=11, color='#374151')
ax.tick_params(axis='both', colors='#374151', labelsize=10)
for spine in ax.spines.values():
spine.set_visible(False)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Dendrogram examples
☕