Search code examples
pythonpython-2.7matplotlib3d

Plotting 3D Cube from Center


I wanted to plot a 3D Cube of Size 1.0 and return a dictionary of Coordinates if the center coordinate is provided.

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

I wanted the result look like:

enter image description here

Problem: 1.) It is returning the Error:

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.) Though the The plot is showing: enter image description here

PS: I am aware of Python/matplotlib : plotting a 3d cube, a sphere and a vector?. But I don't know how to get coordinates from there or define my center.


Solution

  • You should probably use an algorithmic approach instead of hard coding it. More compact (probably) and less mistakes that way. Probably best to define the points as the start point, center point and end point of the lines rather than defining the points then defining the lines. Looking from the top down counterclockwise the pattern is simply ["A","E","B","F","C","G","D","H","A"]. So the lines horizontal lines are AEB (1-3), BFC (1-3), CGD (1-3), and DHA (1-3).