使用 matplotlib 中的平面方程绘制多个 3D 平面

时间:2021-01-29 18:22:43

标签: python numpy matplotlib

我想使用 matplotlib 的 plot_surface 函数绘制多个 3D 平面。 我使用 this link 作为参考。

我的平面完全位于 ZY、ZX 平面。在链接的解决方案中,我发现改变正常情况是可行的。它不适用于我的代码:

    for k in np.arange(len(t)):
    d[k] = -(np.dot(point[k], normal[k].T))
    xx, yy = np.meshgrid(np.arange(np.amin(t[..., 0]), 
             np.amax(t[..., 0] + 1)),
             np.arange(np.amin(t[..., 1]), np.amax(t[..., 1] + 1)))
    if (normal[k][2] == 0) and (normal[k][0] == 0):
        z = (-normal[k][2] * xx - normal[k][0] * yy - d[k]) * 1. / normal[k][1]
    elif (normal[k][2] == 0) and (normal[k][1] == 0):
        z = (-normal[k][1] * xx - normal[k][2] * yy - d[k]) * 1. / normal[k][0]
    else:
        z = (-normal[k][0] * xx - normal[k][1] * yy - d[k]) * 1. / normal[k][2]

    ax.plot_surface(xx, yy, z, color="black", linewidth=0, alpha=0.5)

xx 和 yy 是我想要的范围的网格,k 只是我用来循环所有平面的索引。

我的代码正确吗?如果不是,应该改变什么才能让它工作?有没有更好的方法来实现这一目标?

编辑:预期输出是来自所有平面的立方体,而不是我只在 XY 坐标平面中获取平面。

1 个答案:

答案 0 :(得分:0)

<块引用>

This 作为参考,解决问题。将 initial reference 更新如下,将在 X=0 和 Y=0 时绘制平面。

    d = (np.dot(point, normal))
    #point is (x0,y0,z0) and normal (a,b,c)

    if normal[0] != 0:
        z, yy = np.meshgrid(zmin,zmax,ymin, ymax)
        xx = (-normal[2] * z - normal[1] * yy + d) * 1. / normal[0]

    elif normal[1] != 0:
        xx, z = np.meshgrid(xmin,xmax,zmin, zmax)
        yy = (-normal[0] * xx - normal[2] * z + d) * 1. / normal[1]

    elif normal[2] != 0:
        xx, yy = np.meshgrid(xmin,xmax,ymin, ymax)
        z = (-normal[0] * xx - normal[1] * yy + d) * 1. / normal[2]

    ax.plot_surface(xx, yy, z, color="yellow", linewidth=0, alpha=0.2)