Scatter Plot

Outlier Detection

Scatter with highlighted outliers using distance-based detection.

Output
Outlier Detection
Python
import matplotlib.pyplot as plt
import numpy as np

# === STYLE CONFIG ===
COLORS = {
    'normal': '#94A3B8',
    'outlier': '#EF4444',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9',
}

# === DATA ===
np.random.seed(42)
x = np.random.normal(50, 10, 100)
y = np.random.normal(50, 10, 100)

# Add outliers
x = np.append(x, [85, 90, 15, 10])
y = np.append(y, [85, 15, 90, 10])

# Detect outliers (simple distance from center)
dist = np.sqrt((x - 50)**2 + (y - 50)**2)
outliers = dist > 35

# === FIGURE ===
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])

# === PLOT ===
# Normal points
ax.scatter(x[~outliers], y[~outliers], s=80, c=COLORS['normal'], 
           alpha=0.6, edgecolors='white', linewidths=1, zorder=2)

# Outliers with glow
ax.scatter(x[outliers], y[outliers], s=250, c=COLORS['outlier'], alpha=0.2, zorder=3)
ax.scatter(x[outliers], y[outliers], s=100, c=COLORS['outlier'], 
           alpha=0.9, edgecolors='white', linewidths=2, 
           label=f'Outliers ({outliers.sum()})', zorder=4)

# === 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.legend(loc='upper center', bbox_to_anchor=(0.5, -0.12),
          frameon=False, fontsize=9, labelcolor=COLORS['text_muted'])

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Pairwise Data

Did this help you?

Support PyLucid to keep it free & growing

Support