Scatter Plot
Residual Analysis
Dual-panel regression with residual visualization.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# === STYLE CONFIG ===
COLORS = {
'points': '#8B5CF6',
'trend': '#1E293B',
'positive': '#10B981',
'negative': '#EF4444',
'background': '#FFFFFF',
'text': '#1E293B',
'text_muted': '#64748B',
'grid': '#F1F5F9',
}
# === DATA ===
np.random.seed(42)
x = np.linspace(1, 10, 35)
y_true = 3 * x + 5
y = y_true + np.random.normal(0, 3, 35)
residuals = y - y_true
# === FIGURE ===
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8), dpi=100,
height_ratios=[2, 1], sharex=True)
for ax in [ax1, ax2]:
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
# === MAIN PLOT ===
# Trend line
ax1.plot(x, y_true, color=COLORS['trend'], linewidth=2, label='True relationship', zorder=2)
# Points with residual coloring
colors = [COLORS['positive'] if r >= 0 else COLORS['negative'] for r in residuals]
ax1.scatter(x, y, s=120, c=colors, alpha=0.15, zorder=3)
ax1.scatter(x, y, s=50, c=colors, alpha=0.8,
edgecolors='white', linewidths=1.5, zorder=4)
# Residual lines
for xi, yi, yt in zip(x, y, y_true):
ax1.plot([xi, xi], [yi, yt], color=COLORS['text_muted'],
linewidth=1, alpha=0.4, zorder=1)
ax1.set_ylabel('Value', fontsize=10, color=COLORS['text'], labelpad=10)
# === RESIDUAL PLOT ===
ax2.axhline(0, color=COLORS['trend'], linewidth=1.5, zorder=1)
ax2.scatter(x, residuals, s=100, c=colors, alpha=0.15, zorder=2)
ax2.scatter(x, residuals, s=40, c=colors, alpha=0.8,
edgecolors='white', linewidths=1.5, zorder=3)
ax2.set_xlabel('X', fontsize=10, color=COLORS['text'], labelpad=10)
ax2.set_ylabel('Residual', fontsize=10, color=COLORS['text'], labelpad=10)
# === STYLING ===
for ax in [ax1, ax2]:
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)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Scatter Plot examples
☕