Event Plot

Cardiac ECG R-Peaks

Multi-lead electrocardiogram R-peak detection

Output
Cardiac ECG R-Peaks
Python
import matplotlib.pyplot as plt
import numpy as np

# Simulate cardiac events (R-peaks)
np.random.seed(42)
leads = ['Lead I', 'Lead II', 'Lead III', 'aVR', 'aVL', 'aVF']
heart_rates = [72, 72, 72, 72, 72, 72]  # BPM

# Generate R-peak times with slight variability
base_intervals = 60 / 72  # seconds between beats
r_peaks = []
for _ in leads:
    intervals = np.random.normal(base_intervals, 0.02, 15)
    peaks = np.cumsum(intervals)
    r_peaks.append(peaks)

# Colors for leads
colors = ['#EF4444', '#F97316', '#EAB308', '#22C55E', '#3B82F6', '#8B5CF6']

# Create figure
fig, ax = plt.subplots(figsize=(12, 6), facecolor='#1a1a2e')
ax.set_facecolor('#1a1a2e')

for i, (peaks, color) in enumerate(zip(r_peaks, colors)):
    # Glow effect
    ax.eventplot(peaks, lineoffsets=i, linelengths=0.7, linewidths=4,
                 colors=color, alpha=0.2)
    ax.eventplot(peaks, lineoffsets=i, linelengths=0.6, linewidths=2,
                 colors=color, alpha=0.6)
    ax.eventplot(peaks, lineoffsets=i, linelengths=0.5, linewidths=1,
                 colors='white', alpha=0.9)

# Heart rate annotation
avg_hr = 60 / np.mean(np.diff(r_peaks[0]))
ax.text(11, 5.8, f'HR: {avg_hr:.0f} BPM', fontsize=12, color='#22C55E', 
        fontweight='bold', bbox=dict(boxstyle='round', facecolor='#1a1a2e', 
        edgecolor='#22C55E', linewidth=2))

# Grid lines (ECG paper style)
for x in np.arange(0, 12, 0.2):
    ax.axvline(x, color='#2d2d4a', linewidth=0.3, alpha=0.5)
for x in np.arange(0, 12, 1):
    ax.axvline(x, color='#3d3d5a', linewidth=0.8, alpha=0.7)

# Styling
ax.set_yticks(range(len(leads)))
ax.set_yticklabels(leads, fontsize=11, fontweight='500', color='white')
ax.set_xlabel('Time (seconds)', fontsize=12, fontweight='500', color='#94A3B8')
ax.set_xlim(0, 12)
ax.set_ylim(-0.5, len(leads) - 0.5)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#3d3d5a')
ax.spines['bottom'].set_color('#3d3d5a')
ax.tick_params(colors='#94A3B8', labelsize=10)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support