0

I have the corner points(vertices) of the cube (each point is in (x y z) format) -

[[0],[0],[0]] , [[2],[0],[0]] , [[0],[2],[0]] , [[2],[2],[0]] ... and so on.. 

Given the 8 vertices how can I plot a cube using these (3*1)Arrays in Matplotlib?

furas
  • 134,197
  • 12
  • 106
  • 148
  • did you check if `Matplotlib` has function to draw cubes? Maybe it will need to draw normal lines. – furas Sep 11 '22 at 13:31
  • found with Google: [How to Draw 3D Cube using Matplotlib in Python? - GeeksforGeeks](https://www.geeksforgeeks.org/how-to-draw-3d-cube-using-matplotlib-in-python/) – furas Sep 11 '22 at 13:32
  • [matplotlib - Plot 3D Cube and Draw Line on 3D in Python - Stack Overflow](https://stackoverflow.com/questions/70911608/plot-3d-cube-and-draw-line-on-3d-in-python) – furas Sep 11 '22 at 13:36
  • 2
    a good question, but there was no attempt made by the OP? https://stackoverflow.com/help/minimal-reproducible-example – D.L Sep 11 '22 at 14:31
  • The usual ways i found online all involve using some randomly generated data and inputting this data in a way i didn't really understand(im a beginner) – Hrishi Chougule Sep 11 '22 at 17:50

1 Answers1

0

Combined with answer here, for a cube given below,

enter image description here

You need to edit vertices as,

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

So, the total code is,

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
    
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Unit cube coordinates
x = [0, 1, 1, 0, 0, 1, 1, 0]
y = [0, 0, 1, 1, 0, 0, 1, 1]
z = [0, 0, 0, 0, 1, 1, 1, 1]

# 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))]
ax.scatter(x,y,z)
ax.add_collection3d(Poly3DCollection(poly3d, facecolors='r', linewidths=1, alpha=0.5))

plt.show()
Congy
  • 13
  • 1