Dendrogram
Radial Gene Cluster Light
Light theme gene expression radial clustering with scipy
Output
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)
genes = ['BRCA1', 'TP53', 'EGFR', 'MYC', 'KRAS', 'PIK3CA', 'PTEN', 'AKT1',
'BRAF', 'NRAS', 'CDK4', 'MDM2', 'ERBB2', 'ALK', 'ROS1', 'MET']
n = len(genes)
expression = np.random.rand(n, 8) * 100
Z = linkage(expression, method='ward')
fig_temp, ax_temp = plt.subplots()
set_link_color_palette(['#F5276C', '#6CF527', '#27D3F5', '#F5B027', '#5314E6'])
dn = dendrogram(Z, labels=genes, no_plot=False, color_threshold=0.65*max(Z[:,2]),
above_threshold_color='#9ca3af', ax=ax_temp)
plt.close(fig_temp)
icoord = np.array(dn['icoord'])
dcoord = np.array(dn['dcoord'])
colors = dn['color_list']
x_min, x_max = icoord.min(), icoord.max()
y_max = dcoord.max()
def to_polar(x, y):
theta = (x - x_min) / (x_max - x_min) * 2 * np.pi * 0.9 + np.pi * 0.05
r = y / y_max * 0.6 + 0.35
return theta, r
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw={'projection': 'polar'}, facecolor='#ffffff')
ax.set_facecolor('#ffffff')
# Distance rings
for r in [0.5, 0.7, 0.9]:
circle_theta = np.linspace(0, 2*np.pi, 100)
ax.plot(circle_theta, [r]*100, color='#e5e7eb', linewidth=0.8, linestyle='--', alpha=0.8)
for ic, dc, color in zip(icoord, dcoord, colors):
thetas, rs = [], []
for x, y in zip(ic, dc):
t, r = to_polar(x, y)
thetas.append(t)
rs.append(r)
ax.plot([thetas[0], thetas[1]], [rs[0], rs[1]], color=color, linewidth=2.5, alpha=0.9)
ax.plot([thetas[2], thetas[3]], [rs[2], rs[3]], color=color, linewidth=2.5, alpha=0.9)
if thetas[1] != thetas[2]:
arc_thetas = np.linspace(min(thetas[1], thetas[2]), max(thetas[1], thetas[2]), 40)
ax.plot(arc_thetas, [rs[1]]*len(arc_thetas), color=color, linewidth=2.5, alpha=0.9)
leaf_positions = np.linspace(x_min, x_max, n)
for i, (pos, label) in enumerate(zip(leaf_positions, dn['ivl'])):
theta, _ = to_polar(pos, 0)
rotation = np.degrees(theta) - 90 if np.pi/2 < theta < 3*np.pi/2 else np.degrees(theta) + 90
ha = 'right' if np.pi/2 < theta < 3*np.pi/2 else 'left'
ax.text(theta, 0.22, label, ha=ha, va='center', fontsize=8, color='#6CF527',
rotation=rotation, rotation_mode='anchor', fontweight='600')
color = ['#F5276C', '#6CF527', '#27D3F5', '#F5B027', '#5314E6'][i % 5]
ax.scatter(theta, 0.35, c=color, s=45, zorder=5, edgecolor='#ffffff', linewidth=1)
ax.set_ylim(0, 1.05)
ax.set_yticklabels([])
ax.set_xticklabels([])
ax.spines['polar'].set_visible(False)
ax.grid(False)
ax.set_title('Gene Expression Radial Clustering', fontsize=14, color='#1f2937', fontweight='bold', y=1.08)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Dendrogram examples
☕