gpt4 book ai didi

python - 使用 Matplotlib a*y + b*x + c 绘制 3D 曲面

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:35 25 4
gpt4 key购买 nike

我是 Matplotlib 新手,我需要在 3d 图中绘制一个平面。我在方程中得到了 a、b 和 c 的值,类似于 1y + 2x + 3

theta = np.array([1,2,3])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(theta[0],theta[1],theta[2])
plt.show()

我知道这不是使用 plot_surface() 函数的正确方法,但我不知道如何使用。

更新 1

我用线框想出了一些办法。

# Plot the plane
X = np.linspace(0,100, 500)
Y = np.linspace(0,100, 500)
Z = np.dot(theta[0],X) + np.dot(theta[1],Y) + theta[2]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X,Y,Z)
plt.show()

但它只显示一条线。

enter image description here

最佳答案

试试这个:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
Z = X + Y * 2 + 3
# Plot the surface.
ax.plot_surface(X, Y, Z, linewidth=0)
plt.show()

您需要首先创建变量的网格,然后计算网格上的函数值。

enter image description here

关于python - 使用 Matplotlib a*y + b*x + c 绘制 3D 曲面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46574613/

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