Scatter Plot
Confidence Interval Regression
Linear fit with 95% and 99% confidence bands and equation.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# === STYLE CONFIG ===
COLORS = {
'points': '#6366F1',
'trend': '#6366F1',
'ci_95': '#6366F1',
'ci_99': '#6366F1',
'background': '#FFFFFF',
'text': '#1E293B',
'text_muted': '#64748B',
'grid': '#F1F5F9',
}
# === DATA ===
np.random.seed(42)
x = np.linspace(0, 10, 40)
y = 2.5 * x + 8 + np.random.normal(0, 4, 40)
# Regression
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
y_pred = p(x)
# Confidence intervals (simplified)
residuals = y - y_pred
std_err = np.std(residuals)
ci_95 = 1.96 * std_err
ci_99 = 2.58 * std_err
x_smooth = np.linspace(0, 10, 100)
y_smooth = p(x_smooth)
# === FIGURE ===
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
# === PLOT ===
# Confidence bands
ax.fill_between(x_smooth, y_smooth - ci_99, y_smooth + ci_99,
color=COLORS['ci_99'], alpha=0.08, label='99% CI', zorder=1)
ax.fill_between(x_smooth, y_smooth - ci_95, y_smooth + ci_95,
color=COLORS['ci_95'], alpha=0.15, label='95% CI', zorder=2)
# Trend line with glow
ax.plot(x_smooth, y_smooth, color=COLORS['trend'], linewidth=6, alpha=0.2, zorder=3)
ax.plot(x_smooth, y_smooth, color=COLORS['trend'], linewidth=2.5, zorder=4)
# Points
ax.scatter(x, y, s=120, c=COLORS['points'], alpha=0.15, zorder=5)
ax.scatter(x, y, s=50, c=COLORS['points'], alpha=0.8,
edgecolors='white', linewidths=1.5, zorder=6)
# Equation annotation
ax.annotate(f'y = {z[0]:.2f}x + {z[1]:.2f}',
xy=(0.05, 0.95), xycoords='axes fraction',
fontsize=10, color=COLORS['text'],
bbox=dict(boxstyle='round,pad=0.5', facecolor='white',
edgecolor=COLORS['grid'], alpha=0.9))
# === STYLING ===
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color(COLORS['grid'])
ax.spines['bottom'].set_color(COLORS['grid'])
ax.yaxis.grid(True, color=COLORS['grid'], linewidth=1)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax.set_xlabel('Independent Variable', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('Dependent Variable', fontsize=10, color=COLORS['text'], labelpad=10)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.12),
ncol=2, frameon=False, fontsize=9, labelcolor=COLORS['text_muted'])
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Scatter Plot examples
☕