KDE Plot
Sleep Quality Score Cumulative KDE
Cumulative distribution of sleep quality scores by age group
Output
Python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
np.random.seed(222)
BG_COLOR = '#0d1117'
TEXT_COLOR = 'white'
# Sleep quality scores (0-100)
young = np.clip(np.random.normal(72, 12, 400), 20, 100)
middle = np.clip(np.random.normal(65, 15, 400), 15, 95)
senior = np.clip(np.random.normal(58, 18, 400), 10, 90)
fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)
x_range = np.linspace(0, 100, 500)
for data, color, label in [(young, '#6CF527', '18-35 years'),
(middle, '#27D3F5', '36-55 years'),
(senior, '#F5276C', '56+ years')]:
kde = gaussian_kde(data)
cdf = np.array([kde.integrate_box_1d(-np.inf, x) for x in x_range])
ax.plot(x_range, cdf, color=color, linewidth=3, label=label)
ax.axhline(0.5, color='#888888', linestyle=':', alpha=0.7)
ax.set_xlabel('Sleep Quality Score', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Cumulative Probability', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Sleep Quality: Cumulative Distribution by Age', fontsize=14, color=TEXT_COLOR, fontweight='bold', pad=15)
ax.tick_params(colors='#888888', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#333333')
ax.legend(facecolor=BG_COLOR, edgecolor='#333333', labelcolor=TEXT_COLOR, fontsize=10, loc='lower right')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕