Clustermap

Single-Cell RNA Clusters

Cell type markers expression in single-cell sequencing data

Output
Single-Cell RNA Clusters
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(789)

markers = ['CD3', 'CD4', 'CD8', 'CD19', 'CD20', 'CD56', 'CD14', 'CD16', 
           'HLA-DR', 'CD34', 'CD38', 'CD45']
clusters = [f'Cluster_{i}' for i in range(1, 11)]

data = np.random.rand(12, 10) * 2
data[0:3, 0:3] += 3
data[3:5, 3:5] += 3
data[5, 5] += 4
data[6:8, 6:8] += 3

df = pd.DataFrame(data, index=markers, columns=clusters)

cell_types = ['T cell']*3 + ['B cell']*2 + ['NK'] + ['Monocyte']*2 + ['Stem']*2
type_palette = {'T cell': '#27D3F5', 'B cell': '#F5276C', 'NK': '#6CF527', 
                'Monocyte': '#F5B027', 'Stem': '#4927F5'}
col_colors = pd.Series(cell_types, index=clusters).map(type_palette)

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

g = sns.clustermap(df, cmap=neon_cmap, col_colors=col_colors,
                   method='ward', metric='euclidean',
                   linewidths=0.3, linecolor='#1a1a2e',
                   figsize=(9, 8), dendrogram_ratio=(0.12, 0.12),
                   cbar_pos=(0.01, 0.08, 0.008, 0.12),
                   tree_kws={'linewidths': 1.5, 'colors': '#F5B027'})

g.fig.patch.set_facecolor('#0a0a0f')
g.ax_heatmap.set_facecolor('#0a0a0f')
g.ax_heatmap.tick_params(colors='white', labelsize=8)
g.ax_row_dendrogram.set_facecolor('#0a0a0f')
g.ax_col_dendrogram.set_facecolor('#0a0a0f')
g.cax.tick_params(colors='white', labelsize=7)

g.fig.suptitle('Single-Cell Marker Expression', color='white', fontsize=13, fontweight='bold', y=1.02)
plt.show()
Library

Matplotlib

Category

Heatmaps & Density

Did this help you?

Support PyLucid to keep it free & growing

Support