Violin Plot

Protein Expression Dynamics

Time-course proteomics with fold change analysis

Output
Protein Expression Dynamics
Python
import matplotlib.pyplot as plt
import numpy as np

# Protein expression levels (log2 intensity)
np.random.seed(42)
conditions = ['Control', 'Treatment\n1h', 'Treatment\n6h', 'Treatment\n24h', 'Recovery\n48h']
# Simulating protein dynamics
expression = [
    np.random.normal(10, 1.5, 80),         # Baseline
    np.random.normal(11, 2, 80),           # Early response
    np.random.normal(14, 2.5, 80),         # Peak
    np.random.normal(12, 2, 80),           # Decline
    np.random.normal(10.5, 1.8, 80),       # Recovery
]

# Time-course colors
colors = ['#6B7280', '#3B82F6', '#8B5CF6', '#F59E0B', '#22C55E']

# Create figure with lab theme
fig, ax = plt.subplots(figsize=(12, 7), facecolor='white')

vp = ax.violinplot(expression, positions=range(len(conditions)), widths=0.7,
                   showmeans=False, showmedians=False, showextrema=False)

for i, body in enumerate(vp['bodies']):
    body.set_facecolor(colors[i])
    body.set_edgecolor('white')
    body.set_linewidth(2)
    body.set_alpha(0.7)

# Fold change reference
baseline = np.median(expression[0])
ax.axhline(baseline, color='#6B7280', linewidth=1.5, linestyle='-', alpha=0.5)
ax.axhline(baseline + 1, color='#22C55E', linewidth=1, linestyle=':', alpha=0.6)
ax.axhline(baseline - 1, color='#EF4444', linewidth=1, linestyle=':', alpha=0.6)
ax.text(4.55, baseline + 1.1, '2x Up', fontsize=8, color='#22C55E')
ax.text(4.55, baseline - 1.1, '2x Down', fontsize=8, color='#EF4444', va='top')

# Connect medians with dynamic response curve
medians = [np.median(e) for e in expression]
ax.plot(range(len(conditions)), medians, color='#1F2937', linewidth=2.5,
        linestyle='-', marker='o', markersize=10, markerfacecolor='white',
        markeredgecolor='#1F2937', markeredgewidth=2, zorder=10)

# Fold change annotations
for i, expr in enumerate(expression):
    median = np.median(expr)
    fold_change = 2 ** (median - baseline)
    
    if i > 0:
        fc_text = f'{fold_change:.1f}x' if fold_change >= 1 else f'{fold_change:.2f}x'
        fc_color = '#22C55E' if fold_change > 1.5 else '#EF4444' if fold_change < 0.7 else '#6B7280'
        ax.text(i, 17.5, fc_text, ha='center', fontsize=10, color=fc_color, fontweight='bold')
    
    # Significance stars (simulated p-values)
    if i > 0:
        p_val = np.random.uniform(0.001, 0.1)
        if p_val < 0.001:
            stars = '***'
        elif p_val < 0.01:
            stars = '**'
        elif p_val < 0.05:
            stars = '*'
        else:
            stars = 'ns'
        ax.text(i, 16.5, stars, ha='center', fontsize=9, 
                color='#374151' if stars != 'ns' else '#9CA3AF')

# Label
ax.text(0.5, 18, 'Fold Change vs Control', fontsize=9, color='#6B7280')

# Styling
ax.set_xticks(range(len(conditions)))
ax.set_xticklabels(conditions, fontsize=10, fontweight='500')
ax.set_ylabel('Log2 Intensity', fontsize=12, fontweight='500', color='#374151')
ax.set_ylim(5, 19)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#E5E7EB')
ax.spines['bottom'].set_color('#E5E7EB')
ax.tick_params(colors='#6B7280', labelsize=10)
ax.yaxis.grid(True, linestyle='--', alpha=0.3, color='#9CA3AF')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support