Area Chart

Kaplan-Meier Survival

Survival curves with confidence bands - R survminer style

Output
Kaplan-Meier Survival
Python
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)
time = np.linspace(0, 60, 100)

# Group survival curves
surv_a = np.exp(-0.02 * time)
surv_b = np.exp(-0.04 * time)

# CI approximation
se_a = 0.05 * np.sqrt(surv_a * (1 - surv_a) / 100)
se_b = 0.05 * np.sqrt(surv_b * (1 - surv_b) / 80)

fig, ax = plt.subplots(figsize=(10, 6), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')

ax.fill_between(time, surv_a - 1.96*se_a, surv_a + 1.96*se_a, alpha=0.25, color='#27D3F5')
ax.fill_between(time, surv_b - 1.96*se_b, surv_b + 1.96*se_b, alpha=0.25, color='#F5276C')
ax.step(time, surv_a, color='#27D3F5', linewidth=2.5, where='post', label='Treatment (n=100)')
ax.step(time, surv_b, color='#F5276C', linewidth=2.5, where='post', label='Control (n=80)')

# Median survival lines
med_a = -np.log(0.5) / 0.02
med_b = -np.log(0.5) / 0.04
ax.axhline(0.5, color='#888888', linewidth=1, linestyle=':')
ax.plot([med_a, med_a], [0, 0.5], color='#27D3F5', linewidth=1, linestyle=':')
ax.plot([med_b, med_b], [0, 0.5], color='#F5276C', linewidth=1, linestyle=':')

ax.text(0.95, 0.15, 'Log-rank p < 0.001', transform=ax.transAxes,
        color='white', fontsize=10, ha='right',
        bbox=dict(boxstyle='round', facecolor='#1a1a2e', edgecolor='#333333'))

ax.set_xlabel('Time (months)', color='white', fontsize=11, fontweight='500')
ax.set_ylabel('Survival Probability', color='white', fontsize=11, fontweight='500')
ax.set_title('Kaplan-Meier Survival Curves', color='white', fontsize=13, fontweight='600', pad=15)
ax.tick_params(colors='#888888', labelsize=9)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#333333')
ax.spines['bottom'].set_color('#333333')
ax.set_xlim(0, 60)
ax.set_ylim(0, 1.05)
ax.legend(facecolor='#0a0a0f', edgecolor='#333333', labelcolor='white', loc='lower left')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support