Ridgeline Plot

Sleep Duration by Age Group

How sleep patterns change across different age groups

Output
Sleep Duration by Age Group
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(42)
ages = ['18-25', '26-35', '36-45', '46-55', '56-65', '65+']
sleep_means = [7.2, 6.8, 6.5, 6.3, 6.5, 7.0]
sleep_stds = [1.2, 1.0, 0.9, 0.8, 0.9, 1.1]

fig, ax = plt.subplots(figsize=(12, 8), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

colors = ['#6CF527', '#27F5B0', '#27D3F5', '#276CF5', '#4927F5', '#F527B0']

x = np.linspace(3, 11, 200)
overlap = 2.2

for i, (age, mean, std, color) in enumerate(zip(ages, sleep_means, sleep_stds, colors)):
    data = np.random.normal(mean, std, 1000)
    kde = stats.gaussian_kde(data)
    y = kde(x) * 6
    y_offset = i * overlap
    
    ax.fill_between(x, y_offset, y + y_offset, alpha=0.85, color=color, edgecolor='#374151', linewidth=0.8)
    ax.text(2.5, y_offset + 0.3, age, fontsize=10, color='#1f2937', va='center', ha='right', fontweight='500')

ax.set_xlim(1.5, 11)
ax.set_ylim(-0.5, len(ages) * overlap + 2)
ax.set_xlabel('Hours of Sleep', color='#1f2937', fontsize=11, fontweight='500')
ax.set_title('Sleep Duration Distribution by Age Group', color='#1f2937', fontsize=14, fontweight='bold', pad=20)
ax.tick_params(colors='#374151', labelsize=9)
ax.set_yticks([])
for spine in ax.spines.values():
    spine.set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.spines['bottom'].set_color('#e5e7eb')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support