Mirror Chart

Treatment Effect: Before vs After

Mirror histogram showing clinical treatment outcomes with effect size annotation

Output
Treatment Effect: Before vs After
Python
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

np.random.seed(123)
BG_COLOR = '#0d1117'

# Health scores
before = np.clip(np.random.normal(52, 18, 600), 10, 85)
after = np.clip(np.random.normal(74, 14, 600), 35, 100)

fig, ax = plt.subplots(figsize=(12, 7), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)

bins = np.linspace(10, 100, 30)
bin_width = bins[1] - bins[0]

# Before (top)
h1, _ = np.histogram(before, bins=bins, density=True)
centers = (bins[:-1] + bins[1:]) / 2
bars1 = ax.bar(centers, h1, width=bin_width*0.85, color='#F54927', alpha=0.7,
               edgecolor='#F54927', linewidth=1.5, label=f'Before (μ={np.mean(before):.1f})')

# After (bottom - mirrored)
h2, _ = np.histogram(after, bins=bins, density=True)
bars2 = ax.bar(centers, -h2, width=bin_width*0.85, color='#6CF527', alpha=0.7,
               edgecolor='#6CF527', linewidth=1.5, label=f'After (μ={np.mean(after):.1f})')

ax.axhline(0, color='#555555', linewidth=2)

# Effect size (Cohen's d)
pooled_std = np.sqrt((np.std(before)**2 + np.std(after)**2) / 2)
cohens_d = (np.mean(after) - np.mean(before)) / pooled_std

# Improvement arrow
ax.annotate('', xy=(np.mean(after), -max(h2)*0.5), xytext=(np.mean(before), max(h1)*0.5),
            arrowprops=dict(arrowstyle='->', color='#F5D327', lw=3, 
                          connectionstyle='arc3,rad=0.3'))

# Stats annotation
improvement = ((np.mean(after) - np.mean(before)) / np.mean(before)) * 100
stats_text = f"Improvement: +{improvement:.0f}% | Cohen's d: {cohens_d:.2f} (Large Effect)"
ax.text(0.5, 0.98, stats_text, transform=ax.transAxes, ha='center', va='top',
        fontsize=11, color='#F5D327', fontweight='bold',
        bbox=dict(boxstyle='round,pad=0.4', facecolor=BG_COLOR, edgecolor='#F5D327', lw=2))

ax.set_xlabel('Health Score', fontsize=12, color='white', fontweight='500')
ax.set_title('Clinical Trial: Treatment Efficacy', fontsize=16, color='white', fontweight='bold', pad=25)

ax.tick_params(colors='#888888', labelsize=10)
ax.set_yticks([])
for spine in ['top', 'right']:
    ax.spines[spine].set_visible(False)
for spine in ['bottom', 'left']:
    ax.spines[spine].set_color('#333333')
ax.legend(loc='upper right', facecolor=BG_COLOR, edgecolor='#333333',
          labelcolor='white', fontsize=10)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support