python:绘制线框图3D长方体

时间:2022-09-10 21:58:41

I want to plot 3d cuboid in python.

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

Input : center (3 points for the center) radius (3 radius values, one for each dimension)

输入:中心(中心3点)半径(3个半径值,每个维度1个)

Ideally it should be a wireframe plot(I need to see whats inside).I am not exactly sure how to go about this. Using python matplotlib or Mayavi is fine.

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

Thanks!

谢谢!

So far I have tried the following code ..but that only draws a cube

到目前为止,我已经试过以下代码。但这只画了一个立方体。

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()

Whats missing in this code is that its only a cube(not a cuboid) and it's only centered around 0 (I actually want to provide the center)

这段代码中缺少的是它的唯一一个立方体(不是长方体),它只集中在0(实际上我想提供中心)

After thinking a little bit I came up with this.Which seems right. Let me know if you think its not correct...this is the simplest possible way without installing myavi,pygame, povray (I had a hard time installing these on ipython, conda,my windows laptop)

想了一会儿,我想到了这个。这似乎是正确的。如果你认为不对,请告诉我……这是不安装avi、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()

3 个解决方案

#1


1  

Here is a wireframe plot for a cuboid.

这是长方体的线框图。

def plot_cuboid(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
    import numpy as np
    ox, oy, oz = center
    l, w, h = size

    x = np.linspace(ox-l/2,ox+l/2,num=10)
    y = np.linspace(oy-w/2,oy+w/2,num=10)
    z = np.linspace(oz-h/2,oz+h/2,num=10)
    x1, z1 = np.meshgrid(x, z)
    y11 = np.ones_like(x1)*(oy-w/2)
    y12 = np.ones_like(x1)*(oy+w/2)
    x2, y2 = np.meshgrid(x, y)
    z21 = np.ones_like(x2)*(oz-h/2)
    z22 = np.ones_like(x2)*(oz+h/2)
    y3, z3 = np.meshgrid(y, z)
    x31 = np.ones_like(y3)*(ox-l/2)
    x32 = np.ones_like(y3)*(ox+l/2)

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    # outside surface
    ax.plot_wireframe(x1, y11, z1, color='b', rstride=1, cstride=1, alpha=0.6)
    # inside surface
    ax.plot_wireframe(x1, y12, z1, color='b', rstride=1, cstride=1, alpha=0.6)
    # bottom surface
    ax.plot_wireframe(x2, y2, z21, color='b', rstride=1, cstride=1, alpha=0.6)
    # upper surface
    ax.plot_wireframe(x2, y2, z22, color='b', rstride=1, cstride=1, alpha=0.6)
    # left surface
    ax.plot_wireframe(x31, y3, z3, color='b', rstride=1, cstride=1, alpha=0.6)
    # right surface
    ax.plot_wireframe(x32, y3, z3, color='b', rstride=1, cstride=1, alpha=0.6)
    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()



def test():
    center = [0, 0, 0]
    length = 32 * 2
    width = 50 * 2
    height = 100 * 2
    plot_cuboid(center, (length, width, height))


if __name__ == '__main__':
    test()

Here is the result. plot cuboid

这里是结果。情节长方体

#2


2  

Everybody forgets about POVray that handles 3D very well. It doesn't render wireframe, though, but you can use a half-transparent texture to see what is inside of the box.

大家都忘记了POVray处理3D的很好。虽然它不渲染线框,但是你可以使用半透明的纹理来查看框内的内容。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os

center='-1, -1, -1'
radius='1, 1, 1'


pov='camera { location <0, 2, -3> look_at  <0, 1,  2> }\n\
light_source { <2, 4, -3> color rgb 1*1.5}\n\
background {color rgb <0.00, 0.00, 0.00>}\n\
box {<'+center+'>, < '+radius+'>\n\
pigment { color rgbt <0.67, 1.00, 0.39, 0.80> }\n\
rotate <52, 6, 0>\n\
scale 0.9\n\
translate <0, 1.2, 1>}\n\
'

f=open('scene.pov', 'w')
f.write(pov)
f.close()

os.system('povray +W400 +H300 +A +FN  scene.pov')

Output "scene.png"

输出“scene.png”

python:绘制线框图3D长方体

You need to read povray's documentation.

您需要阅读povray的文档。

#3


2  

I have encountered the same question, and tried to give a answer as follows.

我遇到了同样的问题,并试图给出如下的答案。

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()

This is the result. matplotlib plot cuboid example

这是结果。matplotlib情节长方体的例子

#1


1  

Here is a wireframe plot for a cuboid.

这是长方体的线框图。

def plot_cuboid(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
    import numpy as np
    ox, oy, oz = center
    l, w, h = size

    x = np.linspace(ox-l/2,ox+l/2,num=10)
    y = np.linspace(oy-w/2,oy+w/2,num=10)
    z = np.linspace(oz-h/2,oz+h/2,num=10)
    x1, z1 = np.meshgrid(x, z)
    y11 = np.ones_like(x1)*(oy-w/2)
    y12 = np.ones_like(x1)*(oy+w/2)
    x2, y2 = np.meshgrid(x, y)
    z21 = np.ones_like(x2)*(oz-h/2)
    z22 = np.ones_like(x2)*(oz+h/2)
    y3, z3 = np.meshgrid(y, z)
    x31 = np.ones_like(y3)*(ox-l/2)
    x32 = np.ones_like(y3)*(ox+l/2)

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    # outside surface
    ax.plot_wireframe(x1, y11, z1, color='b', rstride=1, cstride=1, alpha=0.6)
    # inside surface
    ax.plot_wireframe(x1, y12, z1, color='b', rstride=1, cstride=1, alpha=0.6)
    # bottom surface
    ax.plot_wireframe(x2, y2, z21, color='b', rstride=1, cstride=1, alpha=0.6)
    # upper surface
    ax.plot_wireframe(x2, y2, z22, color='b', rstride=1, cstride=1, alpha=0.6)
    # left surface
    ax.plot_wireframe(x31, y3, z3, color='b', rstride=1, cstride=1, alpha=0.6)
    # right surface
    ax.plot_wireframe(x32, y3, z3, color='b', rstride=1, cstride=1, alpha=0.6)
    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()



def test():
    center = [0, 0, 0]
    length = 32 * 2
    width = 50 * 2
    height = 100 * 2
    plot_cuboid(center, (length, width, height))


if __name__ == '__main__':
    test()

Here is the result. plot cuboid

这里是结果。情节长方体

#2


2  

Everybody forgets about POVray that handles 3D very well. It doesn't render wireframe, though, but you can use a half-transparent texture to see what is inside of the box.

大家都忘记了POVray处理3D的很好。虽然它不渲染线框,但是你可以使用半透明的纹理来查看框内的内容。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os

center='-1, -1, -1'
radius='1, 1, 1'


pov='camera { location <0, 2, -3> look_at  <0, 1,  2> }\n\
light_source { <2, 4, -3> color rgb 1*1.5}\n\
background {color rgb <0.00, 0.00, 0.00>}\n\
box {<'+center+'>, < '+radius+'>\n\
pigment { color rgbt <0.67, 1.00, 0.39, 0.80> }\n\
rotate <52, 6, 0>\n\
scale 0.9\n\
translate <0, 1.2, 1>}\n\
'

f=open('scene.pov', 'w')
f.write(pov)
f.close()

os.system('povray +W400 +H300 +A +FN  scene.pov')

Output "scene.png"

输出“scene.png”

python:绘制线框图3D长方体

You need to read povray's documentation.

您需要阅读povray的文档。

#3


2  

I have encountered the same question, and tried to give a answer as follows.

我遇到了同样的问题,并试图给出如下的答案。

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()

This is the result. matplotlib plot cuboid example

这是结果。matplotlib情节长方体的例子