0

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.

Community
  • 1
  • 1
Devashish Das
  • 241
  • 7
  • 19
  • Out of curiosity, what you are trying to do with "return {i:eval(i) for i in hold}"? – Nuclearman Jul 03 '14 at 07:58
  • Problem states to capture motion towards a direction of center particle. So, I was trying to check which way it is moving by the help of a cube. – Devashish Das Jul 03 '14 at 08:10

2 Answers2

2

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

Nuclearman
  • 5,029
  • 1
  • 19
  • 35
0

This is my code.

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()
Ethan Kao
  • 1
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 13 '23 at 05:32
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney May 16 '23 at 00:11