Smoothing with a Savitzky-Golay filter

In [2]:
import scipy.signal
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2*np.pi, 100)
y_noise = np.sin(-x) + (np.random.random(100)-0.5) * 0.5
y_true = np.sin(-x)

y_sg = scipy.signal.savgol_filter(y_noise, 51, 3)

plt.figure(figsize=(9, 6))
plt.plot(x, y_true, color='k', alpha=0.5)
plt.plot(x, y_noise, color='orange')
plt.plot(x, y_sg, color='r')
plt.show()