0

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.

Zhidong Li
  • 61
  • 1
  • 9
  • 3
    There's no such thing as "fitting a curve without a model". Whatever function or method you use to interpolate between the original data points, that's your model - in the case of `splprep` the model is a set of splines. You just need to choose a model that suits your needs. Have you tried adjusting the [parameters of splprep](https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.splprep.html)? – nekomatic Jul 19 '18 at 08:46
  • @nekomatic Thank you for your comment, I did flip a bit at my first attempt. After your remind, I tried a bigger range of parameter s in my data. It appears to improve when s is 6e12 in my case. By the way, how can I extract arbitrary points from the output of splev? – Zhidong Li Jul 19 '18 at 08:59
  • I think you want to use [splev](https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.splev.html#scipy.interpolate.splev). – nekomatic Jul 19 '18 at 09:17
  • Would you please post a link to the data? I will run it through my web site's 3D surface "function finder" and see if it can find a smooth surface equation that is fits the data well. – James Phillips Jul 19 '18 at 13:04

0 Answers0