Dendrogram
Multi-Level Threshold
Dendrogram with multiple cluster levels
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage, set_link_color_palette
np.random.seed(555)
data = np.random.randn(25, 6)
labels = [f'S{i}' for i in range(1, 26)]
Z = linkage(data, method='ward')
fig, ax = plt.subplots(figsize=(14, 7), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')
set_link_color_palette(['#F5276C', '#F5B027', '#6CF527', '#27D3F5', '#4927F5', '#F527B0'])
dn = dendrogram(Z, labels=labels, leaf_rotation=90, leaf_font_size=8,
color_threshold=6, above_threshold_color='#333333', ax=ax)
# Multiple threshold lines
ax.axhline(y=3, color='#27D3F5', linestyle='--', linewidth=1, alpha=0.6)
ax.axhline(y=6, color='#F5B027', linestyle='--', linewidth=1, alpha=0.6)
ax.axhline(y=9, color='#F5276C', linestyle='--', linewidth=1, alpha=0.6)
ax.text(24, 3.2, '5 clusters', color='#27D3F5', fontsize=8)
ax.text(24, 6.2, '3 clusters', color='#F5B027', fontsize=8)
ax.text(24, 9.2, '2 clusters', color='#F5276C', fontsize=8)
ax.set_title('Multi-Level Cluster Analysis', color='white', fontsize=14, fontweight='bold', pad=15)
ax.set_ylabel('Ward Distance', color='#888888', fontsize=11)
ax.tick_params(axis='both', colors='white', labelsize=8)
for spine in ax.spines.values():
spine.set_color('#333333')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Dendrogram examples
☕