Bar Chart

Fold Change Analysis

Horizontal bars with log fold change and significance.

Output
Fold Change Analysis
Python
import matplotlib.pyplot as plt
import numpy as np

# === STYLE CONFIG ===
COLORS = {
    'primary': '#3B82F6',
    'sig': '#10B981',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9',
}

# === DATA ===
biomarkers = ['Biomarker A', 'Biomarker B', 'Biomarker C', 
              'Biomarker D', 'Biomarker E', 'Biomarker F']
fold_change = [2.5, 1.8, -1.5, 3.2, -2.1, 1.2]
errors = [0.4, 0.3, 0.2, 0.5, 0.4, 0.2]
significant = [True, True, False, True, True, False]

y = np.arange(len(biomarkers))
colors = [COLORS['sig'] if s else COLORS['primary'] for s in significant]

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

# === PLOT ===
# Glow
for i, (fc, c) in enumerate(zip(fold_change, colors)):
    ax.barh(i, fc, height=0.55, color=c, alpha=0.15, zorder=1)

# Bars
bars = ax.barh(y, fold_change, height=0.45, color=colors, alpha=0.85,
               edgecolor='white', linewidth=2, zorder=3)

# Error bars
ax.errorbar(fold_change, y, xerr=errors, fmt='none', ecolor=COLORS['text'],
            elinewidth=1.5, capsize=4, capthick=1.5, zorder=4)

# Significance stars
for i, (fc, sig) in enumerate(zip(fold_change, significant)):
    if sig:
        offset = 0.6 if fc > 0 else -0.6
        ha = 'left' if fc > 0 else 'right'
        ax.text(fc + errors[i] + 0.15 if fc > 0 else fc - errors[i] - 0.15, 
                i, '*', ha=ha, va='center', fontsize=14, 
                fontweight='bold', color=COLORS['sig'])

# Reference line at 0
ax.axvline(0, color=COLORS['text'], linewidth=1, zorder=2)

# === STYLING ===
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_color(COLORS['grid'])

ax.xaxis.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_yticks(y)
ax.set_yticklabels(biomarkers)
ax.set_xlim(-3.5, 4.5)
ax.set_xlabel('Log₂ Fold Change', 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