Dendrogram

Banded Cluster Zones Dark

Dendrogram with alternating colored band zones

Output
Banded Cluster Zones Dark
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage, set_link_color_palette

np.random.seed(456)

labels = ['Sample_' + chr(65+i) for i in range(14)]
data = np.random.rand(len(labels), 4) * 80
Z = linkage(data, method='ward')

fig, ax = plt.subplots(figsize=(14, 8), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')

# Add horizontal bands for distance zones
zone_colors = ['#F5276C15', '#27D3F515', '#6CF52715', '#F5B02715']
max_dist = max(Z[:,2])
for i, color in enumerate(zone_colors):
    ax.axhspan(i*max_dist/4, (i+1)*max_dist/4, color=color, zorder=0)

set_link_color_palette(['#F5276C', '#27D3F5', '#6CF527', '#F5B027'])

dn = dendrogram(Z, labels=labels, leaf_rotation=45, leaf_font_size=11,
                color_threshold=0.55*max(Z[:,2]), above_threshold_color='#666666', ax=ax)

ax.set_title('Hierarchical Clustering with Distance Zones', fontsize=15, 
             color='white', fontweight='bold', pad=20)
ax.set_xlabel('Samples', fontsize=11, color='#888888')
ax.set_ylabel('Ward 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')

# Add zone labels
for i, label in enumerate(['Low', 'Medium', 'High', 'Very High']):
    ax.text(-2, (i+0.5)*max_dist/4, label, color='#666666', fontsize=8, va='center', ha='right')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support