Dendrogram

Radial Gradient Background Dark

Dendrogram with radial gradient cluster backgrounds

Output
Radial Gradient Background Dark
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage, fcluster, set_link_color_palette
from matplotlib.patches import Ellipse

np.random.seed(147)

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

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

set_link_color_palette(['#F5276C', '#27D3F5', '#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)

# Add elliptical glows for clusters
clusters = fcluster(Z, t=3, criterion='maxclust')
ellipse_colors = ['#F5276C', '#27D3F5', '#6CF527']
ivl = dn['ivl']
leaves = dn['leaves']

cluster_centers = {}
for i, leaf in enumerate(leaves):
    c = clusters[leaf]
    if c not in cluster_centers:
        cluster_centers[c] = []
    cluster_centers[c].append(i * 10)

for c, positions in cluster_centers.items():
    center_x = np.mean(positions)
    width = (max(positions) - min(positions) + 15)
    color = ellipse_colors[(c-1) % len(ellipse_colors)]
    for scale, alpha in [(1.3, 0.05), (1.15, 0.08), (1.0, 0.12)]:
        ellipse = Ellipse((center_x, max(Z[:,2])*0.4), width*scale, max(Z[:,2])*0.8*scale,
                         facecolor=color, edgecolor='none', alpha=alpha, zorder=0)
        ax.add_patch(ellipse)

ax.set_title('Cluster Analysis with Radial Highlights', fontsize=15, 
             color='white', fontweight='bold', pad=20)
ax.set_xlabel('Clusters', fontsize=11, color='#888888')
ax.set_ylabel('Distance', fontsize=11, color='#888888')
ax.tick_params(axis='both', colors='#888888', labelsize=10)
for spine in ax.spines.values():
    spine.set_visible(False)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support