Stackplot

Genome Composition

DNA base pair distribution.

Output
Genome Composition
Python
import matplotlib.pyplot as plt
import numpy as np

COLORS = {
    'A': '#EF4444',
    'T': '#10B981',
    'G': '#3B82F6',
    'C': '#F59E0B',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9'
}

np.random.seed(42)
position = np.arange(0, 100)
base_a = 25 + 10*np.sin(position/10) + np.random.normal(0, 2, 100)
base_t = 25 + 8*np.sin(position/12 + 1) + np.random.normal(0, 2, 100)
base_g = 25 + 6*np.sin(position/8 + 2) + np.random.normal(0, 2, 100)
base_c = 25 - base_a + 100 - base_t + np.random.normal(0, 2, 100)
base_c = np.clip(100 - base_a - base_t - base_g, 15, 35)

fig, ax = plt.subplots(figsize=(12, 5), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])

ax.stackplot(position, base_a, base_t, base_g, base_c,
             colors=[COLORS['A'], COLORS['T'], COLORS['G'], COLORS['C']],
             alpha=0.85, labels=['Adenine', 'Thymine', 'Guanine', 'Cytosine'])

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color(COLORS['grid'])
ax.spines['bottom'].set_color(COLORS['grid'])
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax.set_xlim(0, 99)
ax.set_xlabel('Position (bp)', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('Frequency (%)', fontsize=10, color=COLORS['text'], labelpad=10)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.12), ncol=4, frameon=False, fontsize=9, labelcolor=COLORS['text_muted'])

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support