I have been trying to use the method described in this post with Poly3DCollection to plot rotated rectangular prisms with just their vertices. Plotting 3D Polygons in python-matplotlib
However, I'm not getting a rectangular/quadrilateral surfaces as I would expect and I get a surface that is weirdly segmented with triangles instead.
If anyone has any help, that'd be appreciated!
for i in range(sample_size):
# Generate randomly sized rectangle
sample_shape = Rectangle(17, 7, 5)
# Rotate rectangle randomly on the z-axis
angle_of_rotation = random.randint(0, 359)
sample_shape.rotate_rectangle(angle_of_rotation)
fig = plt.figure()
ax = Axes3D(fig)
x, y, z = sample_shape.matrix[:, 0], sample_shape.matrix[:, 1], sample_shape.matrix[:, 2]
verts = [list(zip(x, y, z))]
ax.add_collection3d(Poly3DCollection(verts), zs=z)
ax.set_xlim3d(-30, 30)
ax.set_ylim3d(-30, 30)
ax.set_zlim3d(-5, 10)
plt.show()
My rectangle class looks like this
class Rectangle:
def __init__(self, x: float, y: float, z: float):
corner_a = np.array((-x, -y, z))
corner_b = np.array((-x, y, z))
corner_c = np.array((x, y, z))
corner_d = np.array((x, -y, z))
corner_e = np.array((-x, -y, 0))
corner_f = np.array((-x, y, 0))
corner_g = np.array((x, y, 0))
corner_h = np.array((x, -y, 0))
self.matrix = np.array((corner_a, corner_b, corner_c, corner_d,
corner_e, corner_f, corner_g, corner_h))