Bar Chart

Scientific Expression

Gene expression with SEM, significance and individual points.

Output
Scientific Expression
Python
import matplotlib.pyplot as plt
import numpy as np

# === STYLE CONFIG ===
COLORS = {
    'wt': '#6366F1',
    'ko': '#EF4444',
    'rescue': '#10B981',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9',
}

# === DATA ===
conditions = ['WT', 'KO', 'KO + Rescue']
means = [1.0, 0.35, 0.88]
sem = [0.08, 0.05, 0.09]
colors = [COLORS['wt'], COLORS['ko'], COLORS['rescue']]

x = np.arange(len(conditions))

# === FIGURE ===
fig, ax = plt.subplots(figsize=(8, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])

# === PLOT ===
# Glow
for i, (m, c) in enumerate(zip(means, colors)):
    ax.bar(i, m, width=0.55, color=c, alpha=0.15, zorder=1)

# Bars
bars = ax.bar(x, means, width=0.45, color=colors, alpha=0.85,
              edgecolor='white', linewidth=2, zorder=3)

# Error bars (SEM)
ax.errorbar(x, means, yerr=sem, fmt='none', ecolor=COLORS['text'],
            elinewidth=2, capsize=8, capthick=2, zorder=4)

# Significance brackets
h = 0.08
# WT vs KO
ax.plot([0, 0, 1, 1], [1.15, 1.20, 1.20, 0.45], color=COLORS['text'], lw=1.5)
ax.text(0.5, 1.22, '***', ha='center', fontsize=12, fontweight='bold', color=COLORS['text'])

# KO vs Rescue
ax.plot([1, 1, 2, 2], [0.45, 1.08, 1.08, 1.02], color=COLORS['text'], lw=1.5)
ax.text(1.5, 1.10, '**', ha='center', fontsize=12, fontweight='bold', color=COLORS['text'])

# Individual data points (jittered)
np.random.seed(42)
for i, (m, s) in enumerate(zip(means, sem)):
    n_points = 6
    jitter = np.random.uniform(-0.1, 0.1, n_points)
    points = np.random.normal(m, s*2, n_points)
    ax.scatter([i]*n_points + jitter, points, s=30, c=colors[i], 
               alpha=0.4, edgecolors='white', linewidths=0.5, zorder=5)

# === STYLING ===
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.yaxis.grid(True, color=COLORS['grid'], linewidth=1, zorder=0)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)

ax.set_xticks(x)
ax.set_xticklabels(conditions)
ax.set_ylim(0, 1.4)
ax.set_ylabel('Relative Expression', fontsize=10, color=COLORS['text'], labelpad=10)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support