Scatter Plot

Correlation Scatter

Scatter plot with linear regression trend line and R² indicator.

Output
Correlation Scatter
Python
import matplotlib.pyplot as plt
import numpy as np

# === STYLE CONFIG ===
COLORS = {
    'primary': '#3B82F6',
    'trend': '#EF4444',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9',
}

# === DATA ===
np.random.seed(42)
x = np.linspace(0, 10, 50)
y = 2 * x + 3 + np.random.normal(0, 2, 50)

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

# === PLOT ===
# Glow effect
ax.scatter(x, y, s=150, c=COLORS['primary'], alpha=0.1, zorder=1)
ax.scatter(x, y, s=80, c=COLORS['primary'], alpha=0.7, 
           edgecolors='white', linewidths=1.5, zorder=3)

# Trend line
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
ax.plot(x, p(x), color=COLORS['trend'], linewidth=2, 
        linestyle='--', alpha=0.8, label=f'R² = 0.92', zorder=2)

# === 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),
          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