Line/surface of best fit for 3D scatterplot from Excel data

Viewed 31

I have a database of the height, weight, and age of 100s of people. Using matplotlib, I've been able to create a 3D scatterplot of these 3 variables with the xyz co-ordinates of each point representing the (height,weight,age) of one person.

Is it possible to create a (i) line (ii) surface of best fit for the data? The meshgrid would be incomplete since I don't have an age (z) value for each pairing of height and weight (x,y) values. Can we draw the line/surface regardless? Do I have to impute the missing z-values in the meshgrid, and if so, how would I do that?

Most other answers I've seen on this topic assume z is a function of x and y, or that the meshgrid is complete, both of which are not the case here.

1 Answers

Can you try using numpy.meshgrid and then fill your unknown z values with numpy.nan? Matplotlib should ignore numpy.nan from the plots.

By 'best fit' did you mean an interpolation? If so you can pass your data through scipy.interpolate.RectBivariateSpline. I think that would suit your problem?

Related