Area Chart

Hazard Rate Function

Smoothed hazard rate with confidence band - R style

Output
Hazard Rate Function
Python
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)
time = np.linspace(0.1, 50, 200)

# Weibull hazard: h(t) = (k/λ)(t/λ)^(k-1)
k, lam = 1.5, 20
hazard = (k/lam) * (time/lam)**(k-1)
se = 0.1 * hazard * np.sqrt(1/50)  # Approximate SE

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

ax.fill_between(time, hazard - 1.96*se, hazard + 1.96*se, alpha=0.3, color='#F5276C')
ax.plot(time, hazard, color='#F5276C', linewidth=2.5, label='Hazard h(t)')

# Mark hazard at specific times
for t_mark in [10, 25, 40]:
    h_val = (k/lam) * (t_mark/lam)**(k-1)
    ax.scatter([t_mark], [h_val], color='#27D3F5', s=80, zorder=5, edgecolors='white')
    ax.annotate(f'h({t_mark})={h_val:.3f}', (t_mark, h_val), xytext=(5, 10), 
                textcoords='offset points', color='white', fontsize=9)

ax.set_xlabel('Time', color='white', fontsize=11, fontweight='500')
ax.set_ylabel('Hazard Rate h(t)', color='white', fontsize=11, fontweight='500')
ax.set_title('Smoothed Hazard Function (Weibull)', 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.yaxis.grid(True, color='#1a1a2e', linewidth=0.5)
ax.legend(facecolor='#0a0a0f', edgecolor='#333333', labelcolor='white')
ax.set_xlim(0, 50)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support