Hexbin Plot
Gene Expression Levels
Bioinformatics hexbin comparing gene expression under two conditions
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
np.random.seed(789)
control = np.random.lognormal(2, 1, 5000)
treatment = control * np.random.lognormal(0.2, 0.5, 5000)
fig, ax = plt.subplots(figsize=(10, 8), facecolor='#0a0a14')
ax.set_facecolor('#0a0a14')
colors = ['#0a0a14', '#1a1a3f', '#2f2f6f', '#4444af', '#5f5fdf', '#7f7fff', '#9f9fff', '#bfbfff']
cmap = LinearSegmentedColormap.from_list('bio', colors, N=256)
hb = ax.hexbin(np.log2(control), np.log2(treatment), gridsize=30, cmap=cmap, mincnt=1, edgecolors='none')
cbar = plt.colorbar(hb, ax=ax, pad=0.02, shrink=0.8)
cbar.ax.set_facecolor('#0a0a14')
cbar.outline.set_edgecolor('#2f2f6f')
cbar.ax.tick_params(colors='#7f7fff', labelsize=9)
cbar.set_label('Gene Count', color='#7f7fff', fontsize=10)
# Diagonal reference line
lims = [0, 12]
ax.plot(lims, lims, '--', color='#4444af', linewidth=1, alpha=0.7)
ax.set_xlabel('Control (log2)', color='#7f7fff', fontsize=11)
ax.set_ylabel('Treatment (log2)', color='#7f7fff', fontsize=11)
ax.tick_params(colors='#7f7fff', labelsize=10)
for spine in ax.spines.values():
spine.set_visible(False)
plt.tight_layout()
Library
Matplotlib
Category
Pairwise Data
More Hexbin Plot examples
☕