I am looking for something in python to fit a few 3d points to a paraboloid.
I think it can be done with scipy's curve_fit() but i am not able to get that right.
Framing the problem more precisely:
I have a set of x, y and z coordinates and wish to fit a paraboloid in those points as well as plot the entire picture (with points and fitted paraboloid)
I know there are a few questions related to this, but they are not that general and i did not find myself convinced with those answers
Related
I have a 3D plot with a surface z = f(x,y) and a level curve g(x,y) = c. This last one lies in the xy-plane. I need to somehow mark out the points on the surface which lie directly above the level curve. I am trying to maximize f, but are restricted to the points directly above g(x,y)=c. Therefore, for illustrational purposes, it would be nice to make it clear for my readers which points we are evaluating. My current plot looks like this:
Current plot
My first idea was to somehow collect the points on g(x,y) = c, adding them to a list, and then again plot z = f(x,y) with these points as the argument. Is that overcomplicating things?
I know that Python has commands for projecting from surfaces to the xy-plane. What I am trying here is kinda the other way around. Does Python have a command for that?
Answers are highly appreciated.
I've been tasked to develop an algorithm that, given a set of sparse points representing measurements of an existing surface, would allow us to compute the z coordinate of any point on the surface. The challenge is to find a suitable interpolation method that can recreate the 3D surface given only a few points and extrapolate values also outside of the range containing the initial measurements (a notorious problem for many interpolation methods).
After trying to fit many analytic curves to the points I've decided to use RBF interpolation as I thought this will better reproduce the surface given that the points should all lie on it (I'm assuming the measurements have a negligible error).
The first results are quite impressive considering the few points that I'm using.
Interpolation results
In the picture that I'm showing the blue points are the ones used for the RBF interpolation which produces the shape represented in gray scale. The red points are instead additional measurements of the same shape that I'm trying to reproduce with my interpolation algorithm.
Unfortunately there are some outliers, especially when I'm trying to extrapolate points outside of the area where the initial measurements were taken (you can see this in the upper right and lower center insets in the picture). This is to be expected, especially in RBF methods, as I'm trying to extract information from an area that initially does not have any.
Apparently the RBF interpolation is trying to flatten out the surface while I would just need to continue with the curvature of the shape. Of course the method does not know anything about that given how it is defined. However this causes a large discrepancy from the measurements that I'm trying to fit.
That's why I'm asking if there is any way to constrain the interpolation method to keep the curvature or use a different radial basis function that doesn't smooth out so quickly only on the border of the interpolation range. I've tried different combination of the epsilon parameters and distance functions without luck. This is what I'm using right now:
from scipy import interpolate
import numpy as np
spline = interpolate.Rbf(df.X.values, df.Y.values, df.Z.values,
function='thin_plate')
X,Y = np.meshgrid(np.linspace(xmin.round(), xmax.round(), precision),
np.linspace(ymin.round(), ymax.round(), precision))
Z = spline(X, Y)
I was also thinking of creating some additional dummy points outside of the interpolation range to constrain the model even more, but that would be quite complicated.
I'm also attaching an animation to give a better idea of the surface.
Animation
Just wanted to post my solution in case someone has the same problem. The issue was indeed with scipy implementation of the RBF interpolation. I tried instead to adopt a more flexible library, https://rbf.readthedocs.io/en/latest/index.html#.
The results are pretty cool! Using the following options
from rbf.interpolate import RBFInterpolant
spline = RBFInterpolant(X_obs, U_obs, phi='phs5', order=1, sigma=0.0, eps=1.)
I was able to get the right shape even at the edge.
Surface interpolation
I've played around with the different phi functions and here is the boxplot of the spread between the interpolated surface and the points that I'm testing the interpolation against (the red points in the picture).
Boxplot
With phs5 I get the best result with an average spread of about 0.5 mm on the upper surface and 0.8 on the lower surface. Before I was getting a similar average but with many outliers > 15 mm. Definitely a success :)
I created a graph in MATLAB (see figure below) such that around every data point there is a data distribution plotted (grey area plots). The way I did it in MATLAB was to create a set of axes for every distribution curve and then plot the curves without showing those axes at every point of the data curve. I also used a command 'linkaxes' to set figure limits for all the curves at once.
I must say that this is far from an elegant solution and I had many troubles with saving this figure in the correct aspect ratio settings. All in all I couldn't find any other useful option in MATLAB.
Is there a more elegant solution for such types of graphs in Python? I am not that much interested in how to do the areas highlighted, but how to place a set of curves(distributions) exactly at the positions of the main data curve points.
Thank you!
Can someone explain how to make a scatter plot and linear regression from an excel file?
I know how to import the the file with pandas, I know how to do a scatter plot by plugging in my own data in matplotlib, but I don't know how to make python do all three from the file.
Ideally it would also give r value, p value, std error, slope and intercept.
I'm very new to all of this and any help would be great.
I've searched around stack overflow, reddit, and else where, but I haven't found anything recent.
SciPy has a basic linear regression function that fits your criteria: scipy.stats.linregress Just use the appropriate columns from your DataFrame as x and y.
Pyplot's basic plt.plot(x, y) function will give you a line: matplotlib.pyplot.plot. You can compute a set of y values using the slope and intercept.
I am new to python and basically from mechanical background, So i feel it difficult to solve it. kindly help me in this issue.
I have coordinates Xs, Ys, Zs, each having some set of random points. Say 20 each
The coordinates have to be meshed or tessellated within Xs, Ys and Zs.
Then using Gaussian quadrature method or Brute force method we should find the area of each element and sum up to get the total area.
I have went through many references but couldn't find any especially to find the area.
Can anyone help me out in getting a code which can do the above stated.
Check out the scipy integrate module, which has a quadrature function