微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用python进行曲线拟合和平滑处理3D数据

如何解决使用python进行曲线拟合和平滑处理3D数据

我目前正在使用多项式定义的函数来创建3d曲线拟合,但无济于事。 image 1 scatter,image 2 curve fitting 代码如下:

#import excel data 
"""
how can I improve this polynomial function,is there any better methods instead of polynomial? 
"""

def func(data,a,b,c,d):
    x = data[0]
    y = data[1]
    z = data[2]
    return a + b * x + c * y + d * x**2 
# using curve fitting to pass the function 
fittedParameters,pcov = scipy.optimize.curve_fit(
    func,[xData,yData,zData],zData,p0 = None,method= 'lm',maxfev=5000000
) #,maxfev=5000

# making mesh grid 
# making meshgrid
xModel = numpy.linspace( min(x_data),max(x_data),80) #min(x_data)
yModel = numpy.linspace( min(y_data),max(y_data),80)
X,Y = numpy.meshgrid( xModel,yModel )

#popt = fittedparameters
a = fittedParameters[0]
b = fittedParameters[1]
c = fittedParameters[2]
d = fittedParameters[3]
x = X
y = Y
Z = a + b * x + c * y + d * x**2
axes.plot_surface(
    X,Y,Z,rstride=1,cstride=1,cmap=cm.coolwarm,linewidth=1,antialiased=True
)
axes.scatter(x_data,y_data,z_data) # show data along with plotted surface

# add a title for surface plot
axes.set_title('Surface plot of LN(PoF) and length & depth') 


axes.set_xlabel('Depth (mm)')
axes.set_ylabel('Length (mm)')
axes.set_zlabel('LN(PoF)') # Z axis data label

plt.show()

enter image description here

解决方法

内置模块


#%% splprep and splev for the 2D smoothing of x and y value 
def splprep_2d(x,y):
    tck,u = interpolate.splprep([x,y],s = 2,task = 0,full_output=0,quiet = 0,k = 5,t=None)
    fittedParameters = interpolate.splev(u,tck)
    xnew = np.array(fittedParameters[0])
    ynew = np.array(fittedParameters[1])
    return xnew,ynew
xnew,ynew = splprep_2d(x,y)
splprep_2d(x,y)

s = 2是平滑因子,较低的将导致精确绘图,而较高的平滑因子将使曲线平滑。

K =曲线的抛物线性质,可以使用第5条抛物线。

这些是您平滑的参数: xnew = np.array(fittedParameters [0]) ynew = np.array(fittedParameters [1])

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。