Dendrogram

Cutoff Threshold Light

Light dendrogram with prominent cutoff line and shading

Output
Cutoff Threshold 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(321)

labels = ['P' + str(i) for i in range(1, 15)]
data = np.random.rand(len(labels), 6) * 70
Z = linkage(data, method='ward')

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

cutoff = 0.6 * max(Z[:,2])

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

dn = dendrogram(Z, labels=labels, leaf_rotation=45, leaf_font_size=10,
                color_threshold=cutoff, above_threshold_color='#9ca3af', ax=ax)

# Cutoff line with soft shadow
ax.axhline(y=cutoff, color='#F5276C', linewidth=2, linestyle='--', alpha=0.8)
ax.fill_between([ax.get_xlim()[0], ax.get_xlim()[1]], 0, cutoff, 
                color='#27D3F5', alpha=0.06, zorder=0)

ax.annotate('Cluster Cutoff', xy=(ax.get_xlim()[1]-10, cutoff), 
            color='#F5276C', fontsize=10, va='bottom', fontweight='bold')

ax.set_title('Dendrogram with Cluster Cutoff Threshold', fontsize=15, 
             color='#1f2937', fontweight='bold', pad=20)
ax.set_xlabel('Points', fontsize=11, color='#374151')
ax.set_ylabel('Ward 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

Did this help you?

Support PyLucid to keep it free & growing

Support