Area Chart

Sleep Cycle Analysis

Sleep stages throughout the night with REM highlighting

Output
Sleep Cycle Analysis
Python
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)
time = np.linspace(0, 8, 480)  # 8 hours, minute resolution

def sleep_pattern(t):
    cycle = 1.5  # 90 min cycles
    base = 2 + 1.5 * np.sin(2 * np.pi * t / cycle)
    noise = np.random.normal(0, 0.3, len(t))
    return np.clip(base + noise, 0, 4)

stages = sleep_pattern(time)

fig, ax = plt.subplots(figsize=(10, 5), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

ax.fill_between(time, 0, stages, alpha=0.6, color='#4927F5')
ax.plot(time, stages, color='#4927F5', linewidth=1.5)

# REM periods (stage > 3)
rem_mask = stages > 3
ax.fill_between(time, 0, stages, where=rem_mask, alpha=0.8, color='#F5276C', label='REM Sleep')

ax.set_yticks([1, 2, 3, 4])
ax.set_yticklabels(['Deep', 'Light', 'Light', 'REM'])
ax.set_xlabel('Hours of Sleep', color='#1f2937', fontsize=11)
ax.set_title('Sleep Stage Analysis', color='#1f2937', fontsize=14, fontweight='bold', pad=15)
ax.tick_params(colors='#374151', labelsize=9)
for spine in ax.spines.values():
    spine.set_color('#e5e7eb')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.set_xlim(0, 8)
ax.legend(facecolor='#ffffff', edgecolor='#e5e7eb', labelcolor='#1f2937')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support