Line & Scatter

Correlation Analysis

Professional scatter plot with regression line and correlation metrics.

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

COLORS = {
    'points': '#3B82F6',
    'regression': '#1E293B',
    'ci': '#3B82F6',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9',
    'card': '#F8FAFC',
}

np.random.seed(42)
n = 60
x = np.random.uniform(10, 90, n)
y = 0.8 * x + np.random.normal(0, 12, n) + 10

coeffs = np.polyfit(x, y, 1)
r = np.corrcoef(x, y)[0, 1]
fit_line = np.poly1d(coeffs)
x_fit = np.linspace(5, 95, 100)

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

ax.scatter(x, y, color=COLORS['points'], s=50, alpha=0.7,
           edgecolors='white', linewidths=1)

ax.plot(x_fit, fit_line(x_fit), color=COLORS['regression'], 
        linewidth=2, linestyle='--', label='Regression')

y_fit = fit_line(x_fit)
ax.fill_between(x_fit, y_fit - 15, y_fit + 15, color=COLORS['ci'], alpha=0.1)

stats_line1 = 'r = ' + str(round(r, 3))
stats_line2 = 'y = ' + str(round(coeffs[0], 2)) + 'x + ' + str(round(coeffs[1], 1))
stats_text = stats_line1 + chr(10) + stats_line2
bbox = dict(boxstyle='round,pad=0.5', facecolor=COLORS['card'], 
            edgecolor=COLORS['grid'], linewidth=1)
ax.text(0.05, 0.95, stats_text, transform=ax.transAxes, fontsize=10,
        verticalalignment='top', color=COLORS['text'], bbox=bbox, family='monospace')

ax.set_xlim(0, 100)
ax.set_ylim(0, 120)
ax.set_xlabel('Variable X', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('Variable Y', fontsize=10, color=COLORS['text'], labelpad=10)

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.xaxis.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

Did this help you?

Support PyLucid to keep it free & growing

Support