Linear Regression Plot
Screen Time vs Sleep Quality
Digital wellness and rest correlation
Output
Python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
np.random.seed(418)
BG_COLOR = '#ffffff'
TEXT_COLOR = '#1f2937'
n = 75
screen = np.random.uniform(1, 12, n)
sleep_quality = 95 - 5 * screen + np.random.normal(0, 8, n)
sleep_quality = np.clip(sleep_quality, 20, 100)
df = pd.DataFrame({'Screen Time (hrs)': screen, 'Sleep Quality': sleep_quality})
fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)
sns.regplot(
data=df,
x='Screen Time (hrs)',
y='Sleep Quality',
scatter_kws={'color': '#4927F5', 'alpha': 0.6, 's': 55, 'edgecolor': 'white', 'linewidths': 0.5},
line_kws={'color': '#6CF527', 'linewidth': 2.5},
ci=95,
ax=ax
)
for collection in ax.collections[1:]:
collection.set_facecolor('#6CF527')
collection.set_alpha(0.15)
ax.axvline(2, color='#22c55e', linestyle='--', alpha=0.5, linewidth=1)
ax.text(2.2, 30, 'Recommended', color='#22c55e', fontsize=9, rotation=90)
corr = np.corrcoef(screen, sleep_quality)[0, 1]
ax.text(0.95, 0.95, 'r = %.3f' % corr, transform=ax.transAxes, fontsize=12,
color='#6CF527', fontweight='bold', va='top', ha='right',
bbox=dict(boxstyle='round,pad=0.3', facecolor='#f8fafc', edgecolor='#e5e7eb'))
ax.set_xlabel('Daily Screen Time (hours)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Sleep Quality Score', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Screen Time vs Sleep Quality', fontsize=14, color=TEXT_COLOR, fontweight='bold', pad=15)
ax.tick_params(colors='#374151', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#e5e7eb')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Linear Regression Plot examples
☕