Area Chart

GAM Smooth Effect

Generalized additive model smooth term with CI - R style

Output
GAM Smooth Effect
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import UnivariateSpline

np.random.seed(42)
x = np.linspace(0, 10, 100)
true_effect = 2*np.sin(x) + 0.3*x
y = true_effect + np.random.normal(0, 0.8, len(x))

# Fit smooth
spl = UnivariateSpline(x, y, s=5)
y_fit = spl(x)
residuals = y - y_fit
se = np.std(residuals) * np.sqrt(1 + 1/len(x))

fig, ax = plt.subplots(figsize=(10, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

# Rug plot
ax.scatter(x, np.zeros_like(x) - 2.5, marker='|', color='#374151', s=30, alpha=0.5)

ax.fill_between(x, y_fit - 1.96*se, y_fit + 1.96*se, alpha=0.25, color='#6CF527')
ax.scatter(x, y, color='#374151', s=25, alpha=0.5, edgecolors='none')
ax.plot(x, y_fit, color='#6CF527', linewidth=2.5, label='s(x)')
ax.axhline(0, color='#9ca3af', linewidth=1, linestyle='--')

ax.set_xlabel('x', color='#1f2937', fontsize=11, fontweight='500')
ax.set_ylabel('s(x)', color='#1f2937', fontsize=11, fontweight='500')
ax.set_title('GAM Smooth Term: s(x, k=10)', color='#1f2937', fontsize=13, fontweight='600', pad=15)
ax.tick_params(colors='#374151', labelsize=9)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#e5e7eb')
ax.spines['bottom'].set_color('#e5e7eb')
ax.yaxis.grid(True, color='#f3f4f6', linewidth=0.8)
ax.legend(facecolor='#ffffff', edgecolor='#e5e7eb', labelcolor='#1f2937')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support