I am trying to draw a parallelepiped. Actually I started from the python script drawing a cube as:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
points = np.array([[-1, -1, -1],
[1, -1, -1 ],
[1, 1, -1],
[-1, 1, -1],
[-1, -1, 1],
[1, -1, 1 ],
[1, 1, 1],
[-1, 1, 1]])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
r = [-1,1]
X, Y = np.meshgrid(r, r)
ax.plot_surface(X,Y,1, alpha=0.5)
ax.plot_surface(X,Y,-1, alpha=0.5)
ax.plot_surface(X,-1,Y, alpha=0.5)
ax.plot_surface(X,1,Y, alpha=0.5)
ax.plot_surface(1,X,Y, alpha=0.5)
ax.plot_surface(-1,X,Y, alpha=0.5)
ax.scatter3D(points[:, 0], points[:, 1], points[:, 2])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
In order to obtain a parallelepiped, I have multiplied the points matrix by the following matrix:
P =
[[2.06498904e-01 -6.30755443e-07 1.07477548e-03]
[1.61535574e-06 1.18897198e-01 7.85307721e-06]
[7.08353661e-02 4.48415767e-06 2.05395893e-01]]
as:
Z = np.zeros((8,3))
for i in range(8):
Z[i,:] = np.dot(points[i,:],P)
Z = 10.0*Z
My idea is then to represent as follows:
ax.scatter3D(Z[:, 0], Z[:, 1], Z[:, 2])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
And this is what I get:
How can I then put surfaces on these different points to form the parallelepiped (in the way of the cube above)?
Given that the title of this question is 'python draw 3D cube', this is the article I found when I googled that question.
For the purpose of those who do the same as me, who simply want to draw a cube, I have created the following function which takes four points of a cube, a corner first, and then the three adjacent points to that corner.
It then plots the cube.
The function is below:
Giving the result:
Plot surfaces with 3D PolyCollection (example)
See my other answer (https://stackoverflow.com/a/49766400/3912576) for a simpler solution.
Here is a more complicated set of functions which make matplotlib scale better and always forces the input to be a cube.
The first parameter passed to cubify_cube_definition is the starting point, the second parameter is the second point, cube length is defined from this point, the third is a rotation point, it will be moved to match the length of the first and second.
Which produces the following result:
Done using matplotlib and coordinate geometry