简体   繁体   English

Matplotlib 3d 散布 plot 最佳拟合平面

[英]Matplotlib 3d scatter plot plane of best fit

Hello I recently made a 3d scatter plot with python (matplotlib) for my bio class, and I wanted to know how I coukd implement a line of best fit, or even a plane or a circle of best fit. Hello I recently made a 3d scatter plot with python (matplotlib) for my bio class, and I wanted to know how I coukd implement a line of best fit, or even a plane or a circle of best fit. But I can't seem to find anything online.但我似乎无法在网上找到任何东西。 This is my code这是我的代码

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
   
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
    
x = [
    180, 183, 191, 180, 190, 190, 180, 165, 182, 187,
    179, 181, 180, 180, 181, 197, 193, 188, 180, 190,
    188, 191, 185, 175, 158, 180, 193, 186, 165, 172,
    168, 181, 178, 172, 189, 189, 185, 181, 188, 188,
    194, 192, 188, 188, 180, 188, 191, 197, 183
]
y = [
    102, 98, 138, 54, 148, 92, 88, 112, 92, 99, 112, 116,
    104, 81, 94, 120, 108, 80, 110, 90, 120, 100, 142, 108,
    138, 96, 74, 96, 129, 130, 134, 140, 126, 94, 90, 128,
    106, 104, 120, 86, 102, 162, 94, 118, 92, 110, 96,
    102, 98
]
z = [
    162, 168, 175, 175, 169, 179, 152, 165, 182, 158,
    155, 167, 164, 160, 175.7, 180, 158, 186, 176, 160,
    199.7, 172, 180, 162, 173, 180, 152, 171, 163, 169,
    179.832, 170, 173, 172, 173, 180, 170, 170, 170, 164,
    170, 164, 170, 177, 173, 166, 172, 167, 159
]

ax.scatter(x, y, z, c='r', marker='o')
        
ax.set_xlabel('Age')
ax.set_ylabel('Heart Rate')
ax.set_zlabel('Height')
    
plt.show()

For computing the line and plane of best fit you can use the scikit-spatial library.要计算最佳拟合的线和平面,您可以使用scikit-spatial库。

First, convert your coordinates to a Points instance:首先,将坐标转换为Points实例:

import numpy as np

from skspatial.objects import Points

x = [
    180, 183, 191, 180, 190, 190, 180, 165, 182, 187,
    179, 181, 180, 180, 181, 197, 193, 188, 180, 190,
    188, 191, 185, 175, 158, 180, 193, 186, 165, 172,
    168, 181, 178, 172, 189, 189, 185, 181, 188, 188,
    194, 192, 188, 188, 180, 188, 191, 197, 183
]
y = [
    102, 98, 138, 54, 148, 92, 88, 112, 92, 99, 112, 116,
    104, 81, 94, 120, 108, 80, 110, 90, 120, 100, 142, 108,
    138, 96, 74, 96, 129, 130, 134, 140, 126, 94, 90, 128,
    106, 104, 120, 86, 102, 162, 94, 118, 92, 110, 96,
    102, 98
]
z = [
    162, 168, 175, 175, 169, 179, 152, 165, 182, 158,
    155, 167, 164, 160, 175.7, 180, 158, 186, 176, 160,
    199.7, 172, 180, 162, 173, 180, 152, 171, 163, 169,
    179.832, 170, 173, 172, 173, 180, 170, 170, 170, 164,
    170, 164, 170, 177, 173, 166, 172, 167, 159
]

points = Points(np.column_stack((x, y, z)))

To compute the line of best fit do the following:要计算最佳拟合线,请执行以下操作:

from skspatial.objects import Line
from skspatial.plotting import plot_3d

line = Line.best_fit(points)

plot_3d(
    points.plotter(c='r', s=10, depthshade=False),
    line.plotter(t_1=-50, t_2=50),
)

在此处输入图像描述

Similarly, for the plane of best fit:同样,对于最佳拟合平面:

from skspatial.objects import Plane
from skspatial.plotting import plot_3d

plane = Plane.best_fit(points)

plot_3d(
    points.plotter(c='r', s=10, depthshade=False),
    plane.plotter(alpha=0.2, lims_x=(-50, 50), lims_y=(-50, 50)),
)

Finally, the circle of best fit can be computed using the circle-fitting-3d library based on the scikit-spatial library.最后,可以使用基于scikit-spatial库的circle-fitting-3d计算最佳拟合圆。 I think that for these points, a circle is not the right model.我认为对于这些点,一个圆圈不是正确的model。

from circle_fitting_3d import Circle3D
circle_3d = Circle3D(points)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM