Area Chart
LOESS Smoothing
Scatter with LOESS smooth and standard error band - R style
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import UnivariateSpline
np.random.seed(42)
x = np.sort(np.random.uniform(0, 10, 80))
y = 2 + 3*np.sin(x) + np.random.normal(0, 1.5, len(x))
# Smooth using spline (approximation of LOESS)
from scipy.ndimage import gaussian_filter1d
x_smooth = np.linspace(x.min(), x.max(), 200)
spl = UnivariateSpline(x, y, s=len(x)*0.5)
y_smooth = spl(x_smooth)
# Estimate SE from residuals
residuals = y - spl(x)
se = np.std(residuals)
fig, ax = plt.subplots(figsize=(10, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
ax.fill_between(x_smooth, y_smooth - 1.96*se, y_smooth + 1.96*se, alpha=0.25, color='#4927F5', label='95% CI')
ax.scatter(x, y, color='#4927F5', s=40, alpha=0.6, edgecolors='white', linewidth=0.5)
ax.plot(x_smooth, y_smooth, color='#4927F5', linewidth=2.5, label='LOESS smooth')
ax.set_xlabel('x', color='#1f2937', fontsize=11, fontweight='500')
ax.set_ylabel('y', color='#1f2937', fontsize=11, fontweight='500')
ax.set_title('LOESS Smoothing with Confidence Band', 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.xaxis.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
More Area Chart examples
☕