非线性回归散点图

时间:2020-10-14 12:44:20

标签: python matlab plot regression scatter

我的数据点是:

x = [5.00E-07、1.40E-06、4.10E-06、1.25E-05、3.70E-05、1.11E-04、0.33E-04、1.00E-03]
y = [494.55,333.4666667,333.3333333,333.1,303.4966667,197.7533333,66.43333333,67.715]

我的绘图上的x轴必须​​是指数!!

我想制作一条S形的回归线,例如添加的图像。我该怎么做(在matlab或python中)? IMG 更新:我尝试过

import matplotlib.pyplot as plt     
from scipy.interpolate import make_interp_spline     
import numpy as np     

#create data     
x = np.array([5.00E-07, 1.40E-06, 4.10E-06, 1.25E-05, 3.70E-05, 1.11E-04, 3.33E-04, 1.00E-03])     
y= np.array([494.55, 333.4666667, 333.3333333, 333.1, 303.4966667, 197.7533333, 66.43333333, 67.715])     

#define x as 200 equally spaced values between the min and max of original x     
xnew = np.linspace(x.min(), x.max(), 100)     

#define spline      
spl = make_interp_spline(x, y, k=2)     
y_smooth = spl(xnew)    

#create smooth line chart     
plt.plot(x,y, 'o', xnew, y_smooth)    
plt.xscale("log")    
plt.show()    

我的结果是:results 如何使它更平滑?不同的k并不能使其更好。

1 个答案:

答案 0 :(得分:1)

请注意,您对k参数使用的度数越高,曲线越“摇摆”

根据您希望线的弯曲程度,可以修改k的值。

尝试一下:

import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline
import numpy as np

#create data
x = np.array([5.00E-07, 1.40E-06, 4.10E-06, 1.25E-05, 3.70E-05, 1.11E-04, 3.33E-04, 1.00E-03])
y= np.array([494.55, 333.4666667, 333.3333333, 333.1, 303.4966667, 197.7533333, 66.43333333, 67.715])

#define x as 200 equally spaced values between the min and max of original x 
xnew = np.linspace(x.min(), x.max(), 200) 

#define spline
spl = make_interp_spline(x, y, k=3)
y_smooth = spl(xnew)

#create smooth line chart 
plt.plot(xnew, y_smooth)
plt.show()