2D Histogram

Gene Expression Scatter

Control vs treatment expression with fold-change thresholds

Output
Gene Expression Scatter
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

# Gene expression data
np.random.seed(42)
n_genes = 8000

# Control vs treatment expression (log2 scale)
control = np.random.normal(8, 3, n_genes)
control = np.clip(control, 0, 16)

# Most genes unchanged, some up/down regulated
fold_change = np.random.normal(0, 0.8, n_genes)
# Add differentially expressed genes
n_diff = 800
fold_change[:n_diff//2] = np.random.uniform(2, 5, n_diff//2)  # Upregulated
fold_change[n_diff//2:n_diff] = np.random.uniform(-5, -2, n_diff//2)  # Downregulated

treatment = control + fold_change
treatment = np.clip(treatment, 0, 16)

# Genomics dark theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#08080f')
ax.set_facecolor('#08080f')

# Custom colormap - genomics purple-magenta
colors = ['#08080f', '#10102a', '#1a1a4a', '#2a2a6a', '#4a3a8a', 
          '#6a4aaa', '#8a5aca', '#aa6aea', '#ca8aff', '#eaaaff', '#ffccff']
cmap = LinearSegmentedColormap.from_list('genomics', colors, N=256)

# 2D histogram
h = ax.hist2d(control, treatment, bins=50, cmap=cmap, cmin=1)

# Diagonal (no change line)
ax.plot([0, 16], [0, 16], '-', color='#6a4aaa', linewidth=2, alpha=0.7, label='No Change')

# Fold change thresholds (log2 FC = 1 means 2-fold)
ax.plot([0, 15], [1, 16], '--', color='#ff6b6b', linewidth=1.5, alpha=0.7, label='2-fold Up')
ax.plot([1, 16], [0, 15], '--', color='#4ecdc4', linewidth=1.5, alpha=0.7, label='2-fold Down')

# Styled colorbar
cbar = plt.colorbar(h[3], ax=ax, pad=0.02, shrink=0.85)
cbar.ax.set_facecolor('#08080f')
cbar.set_label('Gene Count', fontsize=11, color='#d0b0e0', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#d0b0e0')
cbar.outline.set_edgecolor('#3a2a5a')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#d0b0e0', fontsize=9)

# Labels
ax.set_xlabel('Control Expression (log2)', fontsize=13, color='#d0b0e0', fontweight='600', labelpad=10)
ax.set_ylabel('Treatment Expression (log2)', fontsize=13, color='#d0b0e0', fontweight='600', labelpad=10)
ax.set_title('Differential Gene Expression Analysis', fontsize=16, color='white', 
             fontweight='bold', pad=20)

# Style axes
ax.tick_params(colors='#b090c0', labelsize=10, length=0)
for spine in ax.spines.values():
    spine.set_visible(False)

ax.legend(loc='upper left', fontsize=9, facecolor='#1a1a3a', edgecolor='#3a2a5a',
          labelcolor='#d0b0e0', framealpha=0.9)

ax.set_xlim(0, 16)
ax.set_ylim(0, 16)
ax.set_aspect('equal')
plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support