Dendrogram
Silhouette Style Clusters Dark
Dendrogram with silhouette-inspired colored 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.collections import PolyCollection
np.random.seed(369)
labels = ['S' + str(i) for i in range(1, 13)]
data = np.random.rand(len(labels), 5) * 70
Z = linkage(data, method='ward')
fig, ax = plt.subplots(figsize=(14, 8), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')
set_link_color_palette(['#27D3F5', '#F5276C', '#6CF527', '#F5B027'])
dn = dendrogram(Z, labels=labels, leaf_rotation=0, leaf_font_size=11,
color_threshold=0.5*max(Z[:,2]), above_threshold_color='#444444', ax=ax)
# Create silhouette-style bars
clusters = fcluster(Z, t=3, criterion='maxclust')
bar_colors = ['#27D3F5', '#F5276C', '#6CF527']
leaves = dn['leaves']
for i, leaf in enumerate(leaves):
c = clusters[leaf]
color = bar_colors[(c-1) % len(bar_colors)]
# Draw gradient bar from bottom
for h, alpha in [(15, 0.3), (10, 0.5), (5, 0.7)]:
ax.bar(i*10, h, width=8, bottom=0, color=color, alpha=alpha, zorder=0)
ax.set_title('Cluster Assignment with Silhouette Bars', fontsize=15,
color='white', fontweight='bold', pad=20)
ax.set_xlabel('Samples', fontsize=11, color='#888888')
ax.set_ylabel('Distance', fontsize=11, color='#888888')
ax.tick_params(axis='both', colors='#888888', labelsize=10)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#333333')
ax.spines['bottom'].set_color('#333333')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Dendrogram examples
☕