Linear Regression Plot
Caffeine vs Reaction Time
Cognitive performance and stimulant effect study
Output
Python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
np.random.seed(415)
BG_COLOR = '#ffffff'
TEXT_COLOR = '#1f2937'
n = 60
caffeine = np.random.uniform(0, 400, n)
reaction = 350 - 0.3 * caffeine + np.random.normal(0, 30, n)
reaction = np.clip(reaction, 150, 450)
df = pd.DataFrame({'Caffeine (mg)': caffeine, 'Reaction Time (ms)': reaction})
fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)
sns.regplot(
data=df,
x='Caffeine (mg)',
y='Reaction Time (ms)',
scatter_kws={'color': '#27D3F5', 'alpha': 0.6, 's': 55, 'edgecolor': 'white', 'linewidths': 0.5},
line_kws={'color': '#F5276C', 'linewidth': 2.5},
ci=95,
ax=ax
)
for collection in ax.collections[1:]:
collection.set_facecolor('#F5276C')
collection.set_alpha(0.15)
ax.axvspan(200, 400, alpha=0.08, color='#ef4444')
ax.text(300, 420, 'High Intake', color='#ef4444', fontsize=9, ha='center')
corr = np.corrcoef(caffeine, reaction)[0, 1]
ax.text(0.05, 0.05, 'r = %.3f' % corr, transform=ax.transAxes, fontsize=12,
color='#F5276C', fontweight='bold', va='bottom',
bbox=dict(boxstyle='round,pad=0.3', facecolor='#f8fafc', edgecolor='#e5e7eb'))
ax.set_xlabel('Caffeine Intake (mg)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Reaction Time (ms)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Caffeine vs Reaction Time', 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
☕