I have a scatter curved data in 3D z=f(x,y)
, I want to fit it a smoothed curve. The fitted curve needs to be able to be extracted points from.
I don't have a model for it and I don't bother to make a model. I was thinking use polyfit
but it seems only to work for 2D data. I have seen an answer that suggests to make one variable as independent and generate the other two w.r.t it, let's say x. I don't think it is a good idea as the relation between y and z is ignored.
I tried using scipy.interpolate.splprep
. I later realised it was spline not fitting.
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
from mpl_toolkits.mplot3d import Axes3D
tck, u = interpolate.splprep([xdata,ydata,zdata], s=2)
x,y,z = interpolate.splev(u,tck)
fig1 = plt.figure(1)
ax3d = fig1.add_subplot(111, projection='3d')
ax3d.plot(xdata,ydata,zdata, 'bo')
ax3d.plot(x,y,z, 'r-')
Is there a way to make interpolate.splprep smoother? Or any other method to fit a 3D curve?
Edit I have managed to make the curve smoother by increase quite a bit of s. The x,y,z given by splev are distributed unevenly like the original data. How can I extract a even spread data from the smoothed curve. Or I mean how I can get the smoothed spline model by splprep, I can np.linspace x and y then sub to the model and get a smoothed data set.
This is my data. A curve
Fitting a polynomial using np.polyfit in 3 dimensions
Follow the second answer of this question, I managed to get a coefficient result by sklearn
. But I don't know how to use it. How can I used it to get the smoothed data?
I am frustrating not able to find a way plot or extract data from it.