Clustermap

Gene Expression Clustermap

Hierarchical clustering of gene expression data with sample annotations

Output
Gene Expression Clustermap
Python
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

np.random.seed(42)

# Simulated gene expression data
genes = [f'Gene_{i}' for i in range(1, 26)]
samples = [f'Sample_{i}' for i in range(1, 16)]
data = np.random.randn(25, 15)

# Add some structure (gene clusters)
data[:8, :5] += 2
data[8:16, 5:10] += 2
data[16:, 10:] += 2

df = pd.DataFrame(data, index=genes, columns=samples)

# Sample annotations (treatment groups)
treatment = ['Control']*5 + ['Treatment_A']*5 + ['Treatment_B']*5
palette = {'Control': '#27D3F5', 'Treatment_A': '#F5276C', 'Treatment_B': '#6CF527'}
col_colors = pd.Series(treatment, index=samples).map(palette)

# Custom NEON colormap
neon_cmap = LinearSegmentedColormap.from_list('neon', ['#0a0a0f', '#4927F5', '#27D3F5', '#6CF527', '#F5B027'])

plt.figure(figsize=(8, 7))
g = sns.clustermap(df, cmap=neon_cmap, col_colors=col_colors, 
                   method='ward', metric='euclidean',
                   linewidths=0.5, linecolor='#1a1a2e',
                   figsize=(8, 7), dendrogram_ratio=(0.15, 0.15),
                   cbar_pos=(0.01, 0.08, 0.008, 0.12),
                   tree_kws={'linewidths': 1.5, 'colors': '#27D3F5'})

g.fig.patch.set_facecolor('#0a0a0f')
g.ax_heatmap.set_facecolor('#0a0a0f')
g.ax_heatmap.tick_params(colors='white', labelsize=8)
g.ax_heatmap.set_xlabel('Samples', color='white', fontsize=10)
g.ax_heatmap.set_ylabel('Genes', color='white', fontsize=10)

# Style dendrograms
for ax in [g.ax_row_dendrogram, g.ax_col_dendrogram]:
    ax.set_facecolor('#0a0a0f')
    
g.fig.suptitle('Gene Expression Clustering', color='white', fontsize=14, fontweight='bold', y=1.02)


plt.show()
Library

Matplotlib

Category

Heatmaps & Density

Did this help you?

Support PyLucid to keep it free & growing

Support