python : plotting a wireframe 3D cuboid

标签 python plot mayavi wireframe

我想在 python 中绘制 3d 长方体。

输入: 中心(中心3分) radius(3个半径值,每个维度一个)

理想情况下,它应该是一个线框图(我需要看看里面有什么)。我不太确定该怎么做。使用 python matplotlib 或 Mayavi 都可以。

谢谢!

到目前为止我已经尝试了下面的代码..但是它只绘制了一个立方体

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("equal")

#draw cube
r = [-1, 1]
for s, e in combinations(np.array(list(product(r,r,r))), 2):
    if np.sum(np.abs(s-e)) == r[1]-r[0]:
        ax.plot3D(*zip(s,e), color="b")
plt.show()

这段代码中缺少的是它只是一个立方体(不是长方体)并且它仅以 0 为中心(我实际上想提供中心)

经过一番思考,我想到了这个。这似乎是对的。如果您认为它不正确,请告诉我...这是最简单的方法,无需安装 myavi、pygame、povray(我很难在 ipython、conda、我的 Windows 笔记本电脑上安装这些)

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("equal")

#draw cube

r1 = [-1, 1]
r2 = [-2, 2]
r3 = [-3, 3]
center =[5,5,5]

for s, e in combinations(np.array(list(product(r1,r2,r3))), 2):
    s=np.array(center)+np.array(s)
    e=np.array(center)+np.array(e)
    ax.scatter3D(*center, color="r") 
    if np.linalg.norm(s-e) == 2*r1[1] or np.linalg.norm(s-e) == 2*r2[1] or np.linalg.norm(s-e) == 2*r3[1]:
        print zip(s,e)
        ax.plot3D(*zip(s,e), color="b")  
plt.show()

最佳答案

我也遇到过同样的问题,尝试给出如下答案。

def cuboid_data(center, size):
"""
   Create a data array for cuboid plotting.


   ============= ================================================
   Argument      Description
   ============= ================================================
   center        center of the cuboid, triple
   size          size of the cuboid, triple, (x_length,y_width,z_height)
   :type size: tuple, numpy.array, list
   :param size: size of the cuboid, triple, (x_length,y_width,z_height)
   :type center: tuple, numpy.array, list
   :param center: center of the cuboid, triple, (x,y,z)


  """


    # suppose axis direction: x: to left; y: to inside; z: to upper
    # get the (left, outside, bottom) point
    o = [a - b / 2 for a, b in zip(center, size)]
    # get the length, width, and height
    l, w, h = size
    x = [[o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in bottom surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in upper surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in outside surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]]]  # x coordinate of points in inside surface
    y = [[o[1], o[1], o[1] + w, o[1] + w, o[1]],  # y coordinate of points in bottom surface
         [o[1], o[1], o[1] + w, o[1] + w, o[1]],  # y coordinate of points in upper surface
         [o[1], o[1], o[1], o[1], o[1]],          # y coordinate of points in outside surface
         [o[1] + w, o[1] + w, o[1] + w, o[1] + w, o[1] + w]]    # y coordinate of points in inside surface
    z = [[o[2], o[2], o[2], o[2], o[2]],                        # z coordinate of points in bottom surface
         [o[2] + h, o[2] + h, o[2] + h, o[2] + h, o[2] + h],    # z coordinate of points in upper surface
         [o[2], o[2], o[2] + h, o[2] + h, o[2]],                # z coordinate of points in outside surface
         [o[2], o[2], o[2] + h, o[2] + h, o[2]]]                # z coordinate of points in inside surface
    return x, y, z



def test():
    import matplotlib as mpl
    from mpl_toolkits.mplot3d import Axes3D
    import numpy as np
    center = [0, 0, 0]
    length = 32 * 2
    width = 50 * 2
    height = 100 * 2
    import matplotlib.pyplot as plt
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    X, Y, Z = cuboid_data(center, (length, width, height))
    ax.plot_surface(X, Y, Z, color='b', rstride=1, cstride=1, alpha=0.1)
    ax.set_xlabel('X')
    ax.set_xlim(-100, 100)
    ax.set_ylabel('Y')
    ax.set_ylim(-100, 100)
    ax.set_zlabel('Z')
    ax.set_zlim(-100, 100)
    plt.show()


if __name__ == '__main__':
    test()

这是结果:

matplotlib plot cuboid example

关于 python : plotting a wireframe 3D cuboid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30715083/

相关文章:

python - 如何预测特征数量是否与测试集中可用的特征数量不匹配?

python - 来自 NumPy 数组的 VisPy 动画逐点

python - 如何通过 conda 在 Windows 上为 python 3 安装 mayavi?

python Pandas : how to return an empty dataframe in a loop?

python - 从 django 查询集计算百分位数

python - 如何读取包含默认参数值的函数签名?

r - 将scale_colour_gradient转换为R中ggplot2中的log2空间

plot - 使用 J 创建散点图矩阵

logging - Mathematica 中对数图刻度线的指数形式

python - 使用mlab在mayavi中用文本注释许多点