Linear Regression Plot
Study Hours vs Exam Score
Educational performance correlation analysis
Output
Python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
np.random.seed(403)
BG_COLOR = '#0a0a0f'
TEXT_COLOR = 'white'
n = 90
hours = np.random.uniform(1, 12, n)
score = 45 + 4.5 * hours + np.random.normal(0, 8, n)
score = np.clip(score, 0, 100)
df = pd.DataFrame({'Study Hours': hours, 'Exam Score': score})
fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)
sns.regplot(
data=df,
x='Study Hours',
y='Exam Score',
scatter_kws={'color': '#4927F5', 'alpha': 0.7, 's': 55, 'edgecolor': 'white', 'linewidths': 0.5},
line_kws={'color': '#27F5B0', 'linewidth': 2.5},
ci=95,
ax=ax
)
for collection in ax.collections[1:]:
collection.set_facecolor('#27F5B0')
collection.set_alpha(0.15)
ax.axhline(70, color='#22c55e', linestyle='--', alpha=0.5, linewidth=1)
ax.text(11.5, 71, 'Pass', color='#22c55e', fontsize=9)
corr = np.corrcoef(hours, score)[0, 1]
ax.text(0.05, 0.95, 'r = %.3f' % corr, transform=ax.transAxes, fontsize=12,
color='#27F5B0', fontweight='bold', va='top',
bbox=dict(boxstyle='round,pad=0.3', facecolor='#1a1a2e', edgecolor='#333'))
ax.set_xlabel('Study Hours', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Exam Score', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Study Time vs Performance', fontsize=14, color=TEXT_COLOR, fontweight='bold', pad=15)
ax.tick_params(colors='#888', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#333')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Linear Regression Plot examples
☕