Reputation: 36704
I made a square in 3 dimensions that is essentially a 3d version of this:
[[0., 0., 0., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]]
You can see a 3x3 square of ones. In 3d, in a plot, it gives this:
import numpy as np
import matplotlib.pyplot as plt
square = np.ones((8, 8, 8))
x, y, z = np.where(square ==1)
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, alpha=.8, s=100, ec='k', color='#2FAA75')
ax.set_xlim(-5, 10), ax.set_ylim(-5, 10), ax.set_zlim(-5, 10)
plt.show()
What I want is this straight square to rotate of various angles, not only 90 degrees.
I know that scipy.spatial.transform.Rotation can do that. Unfortunately, I don't know how to implement it. Expected results: imagine the cube being rotated 45 degrees with respects to the x
and z
axes (for example).
Upvotes: 0
Views: 399
Reputation: 198476
the cube being rotated 45 degrees with respects to the x and z axes
# ...
coords = np.where(square == 1)
coords = np.transpose(coords) # get coordinates into a proper shape
rot = Rotation.from_euler('xz', [45, 45], degrees=True) # create a rotation
coords = rot.apply(coords) # apply the rotation
coords = np.transpose(coords) # get coordinates back to the matplotlib shape
# ...
ax.scatter(*coords, alpha=.8, s=100, ec='k', color='#2FAA75')
randomly rotate a numpy array in 3d?
Replace the rotation with
rot = Rotation.random()
Upvotes: 2