将正弦曲线拟合到python matplotlib中的数据

问题描述

Here is my data in excel 我想将此数据拟合为正弦曲线

这是我的代码,

#Fitting function
def func(x,offset,A,freq,phi):
    return offset + A * np.sin(freq * x + phi)

#Experimental x and y data points   
# test_df is the input excel df 
x_data = test_df['x_data']
y_data = test_df['y_data']


#Plot input data points
plt.plot(x_data,y_data,'bo',label='experimental-data')

# Initial guess for the parameters
initial_guess = [.38,2.3,.76,2.77]    

#Perform the curve-fit
popt,pcov = curve_fit(func,x_data,initial_guess)
print(popt)

#x values for the fitted function
x_fit = np.arange(0.0,31,0.01)

#Plot the fitted function
plt.plot(x_fit,func(x_fit,*popt),'r')

plt.show()

这是图形。

enter image description here

我认为这不是最佳选择。我想提出改善曲线拟合的建议。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)