KDE Plot
Daily Screen Time Distribution
KDE of daily device usage hours.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
np.random.seed(118)
screen = np.random.normal(6, 2.5, 1000)
screen = screen[(screen > 0) & (screen < 16)]
kde = stats.gaussian_kde(screen)
x = np.linspace(0, 16, 500)
y = kde(x)
fig, ax = plt.subplots(figsize=(12, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
for i in range(len(x)-1):
if x[i] < 3:
color = '#6CF527'
elif x[i] < 6:
color = '#27D3F5'
elif x[i] < 9:
color = '#F5B027'
else:
color = '#F54927'
ax.fill_between(x[i:i+2], y[i:i+2], alpha=0.5, color=color)
ax.plot(x, y, color='#F54927', linewidth=3)
ax.axvline(7, color='#F5276C', linestyle='--', linewidth=2, label='Recommended Max (7h)')
mean_time = np.mean(screen)
ax.axvline(mean_time, color='#374151', linestyle='-', linewidth=2)
ax.text(mean_time+0.3, max(y)*0.9, 'Avg: ' + str(round(mean_time, 1)) + 'h', color='#374151', fontsize=10, fontweight='bold')
ax.set_xlabel('Screen Time (hours)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Daily Screen Time Distribution', fontsize=16, color='#1f2937', fontweight='bold', pad=15)
ax.tick_params(colors='#374151', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#d1d5db')
ax.legend(loc='upper right', facecolor='#f9fafb', edgecolor='#d1d5db', labelcolor='#374151')
ax.grid(True, alpha=0.3, color='#e5e7eb')
ax.set_xlim(0, 16)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕