har*_*ins 7 python spline curve-fitting smoothing
我需要绘制最佳拟合的平滑曲线,但我发现的所有方法都使用 scipy.optimize.curve_fit(),这需要知道与 x 和 y 相关的函数。对于基本散点图,是否有更简单的方法?
我试图得到的曲线:
import matplotlib.pyplot as plot
x = range(30)
y = [1, 1, 1, 2, 1, 1, 1, 2, 4, 5, 8, 12, 13, 14, 12, 11, 9, 6, 5, 4, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1 ]
plot.plot(x, y, "o")
plot.show()
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
Typically to smooth without a guess of a generating function, people use a spline. Here's an example using your data:
import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline
import numpy as np
x = range(30)
y = [1, 1, 1, 2, 1, 1, 1, 2, 4, 5, 8, 12, 13, 14, 12, 11, 9, 6, 5, 4, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1 ]
s = UnivariateSpline(x, y, s=5)
xs = np.linspace(0, 29, 100)
ys = s(xs)
plt.plot(x, y, 'o')
plt.plot(xs, ys)
plt.show()
Run Code Online (Sandbox Code Playgroud)
As you've probably guessed, the keyword s is used to set how closely the fit matches the data, where s=0 will go through every point.
Splines basically fit a simple function to local sets of points from the curve and then match the derivatives at the boundaries to connect these local curves so the end result looks smooth.
There are a variety of spline routines to choose from in scipy.
| 归档时间: |
|
| 查看次数: |
8453 次 |
| 最近记录: |