Dendrogram

Multi-level Zones Light

Light theme with multiple threshold zone highlights

Output
Multi-level Zones Light
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage, set_link_color_palette

np.random.seed(987)

labels = ['Obs_' + str(i) for i in range(1, 17)]
data = np.random.rand(len(labels), 5) * 100
Z = linkage(data, method='ward')

fig, ax = plt.subplots(figsize=(15, 8), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

max_d = max(Z[:,2])
thresholds = [0.3*max_d, 0.5*max_d, 0.7*max_d]
zone_colors = ['#6CF52710', '#27D3F510', '#F5B02710', '#F5276C10']

# Fill zones
ax.fill_between([0, 160], 0, thresholds[0], color=zone_colors[0], zorder=0)
ax.fill_between([0, 160], thresholds[0], thresholds[1], color=zone_colors[1], zorder=0)
ax.fill_between([0, 160], thresholds[1], thresholds[2], color=zone_colors[2], zorder=0)
ax.fill_between([0, 160], thresholds[2], max_d*1.05, color=zone_colors[3], zorder=0)

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

dn = dendrogram(Z, labels=labels, leaf_rotation=45, leaf_font_size=9,
                color_threshold=thresholds[1], above_threshold_color='#9ca3af', ax=ax)

# Threshold lines
for t, c in zip(thresholds, ['#6CF527', '#27D3F5', '#F5B027']):
    ax.axhline(y=t, color=c, linewidth=1.5, linestyle='--', alpha=0.7)

ax.set_title('Multi-Level Hierarchical Clustering', fontsize=15, 
             color='#1f2937', fontweight='bold', pad=20)
ax.set_xlabel('Observations', fontsize=11, color='#374151')
ax.set_ylabel('Distance', fontsize=11, color='#374151')
ax.tick_params(axis='both', colors='#374151', labelsize=9)
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

Did this help you?

Support PyLucid to keep it free & growing

Support