matplotlib 从中心绘制三维立方体

v1uwarro  于 2023-05-18  发布在  其他
关注(0)|答案(2)|浏览(198)

我想绘制一个大小为1.0的3D立方体,如果提供了中心坐标,则返回一个坐标字典。

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#Concept.py    

def printer(Coordinates):
    A = B = C = 1.0
    X,Y,Z = Coordinates
    S = 1.0
    Q = 1.0
    O2 = [X,Y,Z]
    O1 = [X, Y - S, Z]
    O3 = [X, Y + S, Z]
    A1 = [X - Q, Y - Q, Z - Q]
    B1 = [X + Q, Y - Q, Z - Q]
    C1 = [X + Q, Y - Q, Z + Q]
    D1 = [X - Q, Y - Q, Z + Q]
    E1 = [X,     Y - Q, Z - Q]
    F1 = [X + Q, Y - Q, Z]
    G1 = [X,     Y - Q, Z + Q]
    H1 = [X - Q, Y - Q, Z]
    A2 = [X - S, Y, Z - S]
    B2 = [X + S, Y, Z - S]
    C2 = [X + S, Y, Z + S]
    D2 = [X - S, Y, Z + S]
    E2 = [X,         Y,   Z - S]
    F2 = [X + S,     Y,   Z]
    G2 = [X,         Y,   Z + S]
    H2 = [X - S,     Y,   Z]
    A3 = [X - Q ,Y + Q, Z - Q]
    B3 = [X + Q ,Y + Q, Z - Q]
    C3 = [X + Q ,Y + Q, Z + Q]
    D3 = [X - Q ,Y + Q, Z + Q]
    E3 = [X,     Y + Q, Z - Q]
    F3 = [X + Q, Y + Q, Z]
    G3 = [X,     Y + Q, Z + Q]
    H3 = [X - Q, Y + Q, Z]
#    print A1, B1, C1, D1, E1, 
#    print F1, G1, H1, O1, A2, 
#    print B2, C2, D2, E2, F2, 
#    print G2, H2,A3, B3, C3, 
#    print D3, E3, F3, G3, H3, O3
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    h = [A1, B1, C1, D1, E1, F1, G1, H1, O1, 
        A2, B2, C2, D2, E2, F2, G2, H2,
        A3, B3, C3, D3, E3, F3, G3, H3, O3]
    hx, hy, hz = zip(*h)
    ax.plot(hx,hy,hz, "o-")
    hold = {"A1": A1,  "B1": B1,  "C1": C1,  "D1": D1,
            "E1": E1,  "F1": F1,  "G1": G1,  "H1": H1,
            "O1": O1,  "A2": A2,  "B2": B2,  "C2": C2,
            "D2": D2,  "E2": E2,  "F2": F2,  "G2": G2,
            "H2": H2,  "A3": A3,  "B3": B3,  "C3": C3,
            "D3": D3,  "E3": E3,  "F3": F3,  "G3": G3,
            "H3": H3,  "O3": O3}
    plt.show()
    return {i:eval(i) for i in hold}

printer([0,0,0])

我希望结果看起来像:

问题:1.)返回错误:

Traceback (most recent call last):
  File "concept.py", line 59, in <module>
    printer([0,0,0])
  File "concept.py", line 57, in printer
    return {i:eval(i) for i in hold}
  File "concept.py", line 57, in <dictcomp>
    return {i:eval(i) for i in hold}
  File "<string>", line 1, in <module>
NameError: name 'O3' is not defined

2.)虽然剧情显示:

PS:我知道Python/matplotlib : plotting a 3d cube, a sphere and a vector?。但我不知道如何从那里得到坐标或定义我的中心。

7xllpg7q

7xllpg7q1#

你可能应该使用一种算法方法,而不是硬编码。更紧凑(可能)和更少的错误。也许最好将点定义为直线的起点、中心点和终点,而不是先定义点然后定义直线。从上往下逆时针看,图案简单地是[“A”,“E”,“B”,“F”,“C”,“G”,“D”,“H”,“A”]。因此,水平线是AEB(1-3)、BFC(1-3)、CGD(1-3)和DHA(1-3)。

0mkxixxg

0mkxixxg2#

这是我的代码。

import matplotlib.pyplot as plt

side_length = 5

width, length, height = side_length, side_length, side_length

def generate_fig(x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4, x5,y5,z5, 
x6,y6,z6, x7,y7,z7, x8,y8,z8, color):
    # Create a 3D figure
    fig = plt.figure("3D_plot")
    ax = fig.add_subplot(111, projection='3d')
    ax.set_title('3D Cube')

    # generate a prism points
    ax.scatter(x1, y1, z1, color=color)
    ax.scatter(x2, y2, z2, color=color)
    ax.scatter(x3, y3, z3, color=color)
    ax.scatter(x4, y4, z4, color=color)
    ax.scatter(x5, y5, z5, color=color)
    ax.scatter(x6, y6, z6, color=color)
    ax.scatter(x7, y7, z7, color=color)
    ax.scatter(x8, y8, z8, color=color)

    # connect points
    x = [x1, x2, x3, x4, x5, x6, x7, x8]
    y = [y1, y2, y3, y4, y5, y6, y7, y8]
    z = [z1, z2, z3, z4, z5, z6, z7, z8]

for i in range(len(x)):
    for j in range(i+1, len(x)):
        xs = [x[i], x[j]]
        ys = [y[i], y[j]]
        zs = [z[i], z[j]]
        ax.plot(xs, ys, zs, c='tab:grey')

generate_fig(1,1,1, 1,height,length, width,1,length, 
width,height,length, 1,1,length, width,1,1, 1,height,1, 
width,height,1, 
'r')

# show the plot
plt.show()

相关问题