Line & Scatter
Residuals Plot
Visualization of residuals from a fitted model.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# === STYLE CONFIG ===
COLORS = {
'positive': '#10B981', # Emerald
'negative': '#F43F5E', # Rose
'zero': '#64748B', # Slate
'background': '#FAFBFC',
'text_muted': '#64748B',
'grid': '#E2E8F0',
}
# === DATA ===
np.random.seed(42)
x = np.linspace(0, 10, 30)
residuals = np.random.normal(0, 1, 30)
# === FIGURE ===
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
# === PLOT ===
colors = [COLORS['positive'] if r >= 0 else COLORS['negative'] for r in residuals]
ax.scatter(x, residuals, c=colors, s=60, edgecolors='white', linewidths=1.5, zorder=3)
# Zero line
ax.axhline(y=0, color=COLORS['zero'], linewidth=1.5, linestyle='--', zorder=2)
# Stem lines
for xi, ri, ci in zip(x, residuals, colors):
ax.plot([xi, xi], [0, ri], color=ci, linewidth=1, alpha=0.5, zorder=1)
# === AXES ===
ax.set_xlim(-0.5, 10.5)
ax.set_ylim(-3, 3)
ax.set_xlabel('Fitted values', fontsize=10, color=COLORS['text_muted'], labelpad=10)
ax.set_ylabel('Residuals', fontsize=10, color=COLORS['text_muted'], labelpad=10)
# === STYLING ===
for spine in ['top', 'right']:
ax.spines[spine].set_visible(False)
for spine in ['bottom', 'left']:
ax.spines[spine].set_color(COLORS['grid'])
ax.yaxis.grid(True, color=COLORS['grid'], linewidth=0.5, alpha=0.7)
ax.xaxis.grid(False)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
# Custom legend
ax.scatter([], [], c=COLORS['positive'], s=60, edgecolors='white', label='Positive')
ax.scatter([], [], c=COLORS['negative'], s=60, edgecolors='white', label='Negative')
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 Line & Scatter examples
☕