numpy 给定立方体的所有顶点,在平面上绘制立方体

3j86kqsm  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(137)

我特灵绘制一个立方体上的平面使用matplotib。
我正在做一个关于无人机(一个无人驾驶飞行器)着陆的项目,有这些测试,我必须想象。我有一个着陆飞机已经绘制如下所示。

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

xs = np.linspace(-10, 10, 100)
ys = np.linspace(-10, 20, 100)

X, Y = np.meshgrid(xs, zs)
Z = 0 / X

fig = plt.figure(figsize=(25,25))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
ax.set_ylim([-40, 40])
ax.set_zlim([0, 10])
ax.set_xlim([-10, 10])
# Hide grid lines
# ax.grid(False)
plt.xlabel("X axis")
plt.ylabel("Y axis")

# Hide axes ticks
# ax.set_xticks([])
# ax.set_yticks([])
# ax.set_zticks([])
# plt.savefig("foo.png")
plt.show()

字符串
输出是下面的图像。


的数据
现在我想在图中绘制一个立方体。我有一个函数可以生成一个立方体的所有顶点,但是我在尝试绘制它时遇到了错误。顶点是从顶部西北,顶部东北,顶部东南,顶部西南生成的。同样适用于底部。
这是函数。

def createVertices(c3, stepSize):
    hs = 0.5 * stepSize
    a = [c3[0]-hs,c3[1]+hs,c3[2]+hs]
    b = [c3[0]+hs,c3[1]+hs,c3[2]+hs]
    c = [c3[0]+hs,c3[1]-hs,c3[2]+hs]
    d = [c3[0]-hs,c3[1]-hs,c3[2]+hs]
    e = [c3[0]-hs,c3[1]+hs,c3[2]-hs]
    f = [c3[0]+hs,c3[1]+hs,c3[2]-hs]
    g = [c3[0]+hs,c3[1]-hs,c3[2]-hs]
    h = [c3[0]-hs,c3[1]-hs,c3[2]-hs]
    
    return [a,b,c,d,e,f,g,h]


我必须动态地做这件事,因为我有超过100个测试用例。任何帮助都将不胜感激。
其他信息:
Jared建议使用almost similar question,但我遇到了来自ax.plot_surface(X,Y,1, alpha=0.5)的错误AttributeError: 'int' object has no attribute 'ndim'

bvpmtnay

bvpmtnay1#

由于Matplotlib第三方包S3Dlib具有预定义的立方体,因此只需使用它来获取:


的数据
其中代码为:

import matplotlib.pyplot as plt
import s3dlib.surface as s3d

# test cases ......
stepSize = [ 3, 6, 1, 4 ]
centers = [ [5,20,6] , [-7,7,7] , [1,1,1], [0,-30,5] ]
colors = [ 'C0', 'C1', 'C2', 'C4']

shade=False  #......................  set True to show solid cubes
for i, ss in enumerate(stepSize) :
    surf = s3d.CubicSurface(0,color=colors[i]).domain( ss/2, ss/2, ss/2 )
    surf.transform(translate=centers[i])
    surf = surf.shade() if shade else surf.edges  
    boxes = surf if i==0 else boxes+surf

landing = s3d.PlanarSurface(0,color='.6').domain(10,10)

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set( xlim = [-10,10], ylim=[-40,40],  zlim=[0,10],
        xlabel='X',      ylabel='Y',     zlabel='Z' )
ax.add_collection3d(landing)
ax.add_collection3d(boxes)
fig.tight_layout()
plt.show()

字符串
它出现在createVertices函数中,立方体中心坐标和宽度被传递进来,所以直接使用它们。

k3bvogb1

k3bvogb12#

我设法解决了动态创建立方体。这是整个脚本。

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

# create an array of all cube's center points
xPoints = np.linspace(30,50,3, dtype = int)
yPoints = np.linspace(-20,20,5, dtype = int)
zPoints = np.linspace(5,35,7, dtype = int)

allPoints= []

for xp in xPoints:
    for yp in yPoints:
        for zp in zPoints:
            allPoints.append([xp,yp,zp])

# plot each of the cube
for c3 in allPoints:
    # figure name
    figName = ""+str(c3[0])+"_"+str(c3[1])+"_"+str(c3[2])+".pdf"
    
    # landing strip
    xs = np.linspace(10, 60, 100)
    ys = np.linspace(-10, 20, 100)

    X, Y = np.meshgrid(xs, ys)
    Z = 0 / X

    # create vertices
    hs = 0.5 * 2
    a = [c3[0]-hs,c3[1]+hs,c3[2]+hs]
    b = [c3[0]+hs,c3[1]+hs,c3[2]+hs]
    c = [c3[0]+hs,c3[1]-hs,c3[2]+hs]
    d = [c3[0]-hs,c3[1]-hs,c3[2]+hs]
    e = [c3[0]-hs,c3[1]+hs,c3[2]-hs]
    f = [c3[0]+hs,c3[1]+hs,c3[2]-hs]
    g = [c3[0]+hs,c3[1]-hs,c3[2]-hs]
    h = [c3[0]-hs,c3[1]-hs,c3[2]-hs]

    # arrange the vertices
    vertices =  [h,g,c,d,e,f,b,a]

    x, y, z = [],[],[]
    for vert in vertices:
        x.append(vert[0])
        y.append(vert[1])
        z.append(vert[2])

     # Face IDs
    vertices = [[0,1,2,3],[1,5,6,2],[3,2,6,7],[4,0,3,7],[5,4,7,6],[4,5,1,0]]
    tupleList = list(zip(x, y, z))

    poly3d = [[tupleList[vertices[ix][iy]] for iy in range(len(vertices[0]))] for ix in range(len(vertices))]

    # finally some plotting ;-)
    fig = plt.figure(figsize=(25,25))
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(x,y,z)
    ax.add_collection3d(Poly3DCollection(poly3d, facecolors='r', linewidths=1, alpha=0.5))
    ax.plot_surface(X, Y, Z)
    ax.set_ylim([-40, 40])
    ax.set_zlim([0, 40])
    ax.set_xlim([0, 60])
    
    # Hide grid lines
    # ax.grid(False)
    
    plt.xlabel("X axis")
    plt.ylabel("Y axis")
    plt.savefig(figName)

字符串
我希望它能帮助别人在未来

相关问题