Event Plot

Sleep Architecture

Overnight sleep stage progression and cycles

Output
Sleep Architecture
Python
import matplotlib.pyplot as plt
import numpy as np

# Simulate sleep stage transitions
np.random.seed(42)
stages = ['Awake', 'REM', 'Light (N1)', 'Light (N2)', 'Deep (N3)']
# Typical sleep cycle events
transitions = [
    np.array([0, 5.5, 6.2, 7.8]),                    # Brief awakenings
    np.array([1.5, 3.0, 4.5, 6.0, 7.5]),            # REM cycles
    np.array([0.2, 0.8, 2.2, 3.5, 5.0, 6.5]),       # N1
    np.array([0.5, 1.2, 2.0, 2.8, 4.0, 5.2, 6.8]),  # N2
    np.array([0.8, 2.5, 4.2]),                       # N3 deep sleep
]

# Sleep stage colors
colors = ['#EF4444', '#8B5CF6', '#60A5FA', '#3B82F6', '#1E40AF']

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

for i, (events, color) in enumerate(zip(transitions, colors)):
    # Duration bars (variable width)
    for j, event in enumerate(events):
        duration = np.random.uniform(0.3, 0.8)
        ax.barh(i, duration, left=event, height=0.6, color=color, alpha=0.7)

# Sleep cycles
for cycle in range(1, 6):
    ax.axvline(cycle * 1.5, color='#6366F1', linewidth=1, linestyle=':', alpha=0.4)

# Time labels
ax.set_xticks(range(9))
ax.set_xticklabels(['22:00', '23:00', '00:00', '01:00', '02:00', 
                    '03:00', '04:00', '05:00', '06:00'], fontsize=9, color='#A5B4FC')

# Quality score
ax.text(8.5, 4.5, 'Sleep\nScore', ha='center', fontsize=10, color='#A5B4FC')
ax.text(8.5, 3.5, '85', ha='center', fontsize=24, fontweight='bold', color='#22C55E')

# Styling
ax.set_yticks(range(len(stages)))
ax.set_yticklabels(stages, fontsize=11, fontweight='500', color='white')
ax.set_xlabel('Time', fontsize=12, fontweight='500', color='#A5B4FC')
ax.set_xlim(0, 9)
ax.set_ylim(-0.5, len(stages) - 0.5)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#4C4680')
ax.spines['bottom'].set_color('#4C4680')
ax.tick_params(colors='#A5B4FC', labelsize=10)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support