Dendrogram
Silhouette Bars Light
Light theme with silhouette-style colored cluster bars
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage, fcluster, set_link_color_palette
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='#ffffff')
ax.set_facecolor('#ffffff')
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='#9ca3af', ax=ax)
# 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)]
ax.bar(i*10, 15, width=8, bottom=0, color=color, alpha=0.25, zorder=0)
ax.bar(i*10, 10, width=8, bottom=0, color=color, alpha=0.35, zorder=0)
ax.set_title('Cluster Assignment with Silhouette Bars', fontsize=15,
color='#1f2937', fontweight='bold', pad=20)
ax.set_xlabel('Samples', fontsize=11, color='#374151')
ax.set_ylabel('Distance', fontsize=11, color='#374151')
ax.tick_params(axis='both', colors='#374151', labelsize=10)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#e5e7eb')
ax.spines['bottom'].set_color('#e5e7eb')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Dendrogram examples
☕