gpt4 book ai didi

python - 如何使用数据集来拟合 3D 表面?

转载 作者:行者123 更新时间:2023-12-05 08:40:19 26 4
gpt4 key购买 nike

我正在尝试将此 X、Y、Z 数据集拟合到未知表面。

不幸的是,线性拟合不足以显示表面数据。我认为多项式拟合可能适合这种情况。另外,问题是我不知道如何建立多项式拟合函数来完成曲面拟合。

任何帮助都会很棒。

谢谢

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

X = [[2, 2, 2], [1.5, 1.5, 1.5], [0.5, 0.5, 0.5]]
Y = [[3, 2, 1], [3, 2, 1], [3, 2, 1]]
Z = [[2.4, 2.5, 2.2], [2.4, 3, 2.5], [4, 3.3, 8]]

# ================= Plot figure ================= ##
Fontsize_set = {'size': 20}
fig = plt.figure(figsize=[8, 5], dpi=140, facecolor='w')
ax = fig.gca(projection='3d')
ax.grid(color='y', linestyle='--', linewidth=0.5)
ax.tick_params(labelsize=20)
ax.set_xlim3d(0, 3)
ax.set_ylim3d(0, 6)
ax.set_zlim3d(0, 10)
ax.view_init(30, 45)
ax.scatter(X, Y, Z, s=50, color='k', marker='o', linewidth=None, alpha=1)
# ax.plot_surface(X, Y, Z)
fig.tight_layout()
plt.show()

enter image description here

最佳答案

给你

=^..^=

代码中的说明:

import numpy as np
from scipy.optimize import curve_fit
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt


# test function
def function(data, a, b, c):
x = data[0]
y = data[1]
return a * (x**b) * (y**c)

# setup test data
raw_data = [2.0, 2.0, 2.0], [1.5, 1.5, 1.5], [0.5, 0.5, 0.5],[3.0, 2.0, 1.0], [3.0, 2.0, 1.0],\
[3.0, 2.0, 1.0], [2.4, 2.5, 2.2], [2.4, 3.0, 2.5], [4.0, 3.3, 8.0]

# convert data into proper format
x_data = []
y_data = []
z_data = []
for item in raw_data:
x_data.append(item[0])
y_data.append(item[1])
z_data.append(item[2])

# get fit parameters from scipy curve fit
parameters, covariance = curve_fit(function, [x_data, y_data], z_data)

# create surface function model
# setup data points for calculating surface model
model_x_data = np.linspace(min(x_data), max(x_data), 30)
model_y_data = np.linspace(min(y_data), max(y_data), 30)
# create coordinate arrays for vectorized evaluations
X, Y = np.meshgrid(model_x_data, model_y_data)
# calculate Z coordinate array
Z = function(np.array([X, Y]), *parameters)

# setup figure object
fig = plt.figure()
# setup 3d object
ax = Axes3D(fig)
# plot surface
ax.plot_surface(X, Y, Z)
# plot input data
ax.scatter(x_data, y_data, z_data, color='red')
# set plot descriptions
ax.set_xlabel('X data')
ax.set_ylabel('Y data')
ax.set_zlabel('Z data')

plt.show()

关于python - 如何使用数据集来拟合 3D 表面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56439930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com