2D Histogram
Sequencing Quality Metrics
Bioinformatics visualization of read depth vs mapping quality in next-generation sequencing data.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
# NGS data
np.random.seed(42)
n_reads = 12000
read_depth = np.random.lognormal(3.5, 0.8, n_reads)
read_depth = np.clip(read_depth, 1, 500)
base_mapq = np.random.beta(8, 2, n_reads) * 60
depth_effect = np.minimum(read_depth / 100, 1) * 10
mapq = np.clip(base_mapq + depth_effect + np.random.normal(0, 3, n_reads), 0, 60)
# Bioinformatics theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#08081a')
ax.set_facecolor('#08081a')
# Custom colormap - DNA magenta
colors = ['#08081a', '#10102a', '#1a1a4a', '#2a2a6a', '#4a3a8a',
'#6a4aaa', '#8a5aca', '#aa7aea', '#ca9aff', '#eabaff', '#ffdaff']
cmap = LinearSegmentedColormap.from_list('dna', colors, N=256)
# 2D histogram
h = ax.hist2d(read_depth, mapq, bins=[50, 40], cmap=cmap, cmin=1)
# Quality thresholds
ax.axhline(y=30, color='#ca9aff', linestyle='--', alpha=0.7, linewidth=2, label='High Quality (MAPQ >= 30)')
ax.axhline(y=20, color='#8a5aca', linestyle=':', alpha=0.6, linewidth=1.5, label='Acceptable (MAPQ >= 20)')
ax.axvline(x=30, color='#6a4aaa', linestyle='--', alpha=0.6, linewidth=1.5, label='Min Depth (30x)')
# Styled colorbar
cbar = plt.colorbar(h[3], ax=ax, pad=0.02, shrink=0.85)
cbar.ax.set_facecolor('#08081a')
cbar.set_label('Read Count', fontsize=11, color='#c0a0e0', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#c0a0e0')
cbar.outline.set_edgecolor('#2a2a4a')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#c0a0e0', fontsize=9)
ax.set_xlabel('Read Depth (x)', fontsize=13, color='#c0a0e0', fontweight='600', labelpad=10)
ax.set_ylabel('Mapping Quality (MAPQ)', fontsize=13, color='#c0a0e0', fontweight='600', labelpad=10)
ax.set_title('NGS Read Depth vs Mapping Quality', fontsize=16, color='white', fontweight='bold', pad=20)
ax.tick_params(colors='#a080c0', labelsize=10, length=0)
for spine in ax.spines.values():
spine.set_visible(False)
ax.legend(loc='lower right', fontsize=9, facecolor='#1a1a3a', edgecolor='#2a2a4a', labelcolor='#c0a0e0')
ax.set_xlim(0, 300)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More 2D Histogram examples
☕