Might be a simple question but let's say you have 2d normal data that you want to rotate 90 degrees counter clockwise, to do this you can use rotation matrices and construct one with theta = np.pi / 2
and then multiply the data by your rotation matrix. That works great, however when I try to rotate the data 45 degrees (np.pi / 4
), it does not work. It appears to have rotated the data clockwise, but flipping the sign of the angle does not change the resulting plot. How can I rotate the data 45 degrees counterclockwise?
cov = np.array([[1, .7], [.7, 1]])
data = np.random.multivariate_normal(np.zeros(2), cov, size=10000)
theta = np.pi / 4
rot_matrix = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
data_rot = (rot_matrix @ data.T).T
fig, axes = plt.subplots(2)
axes[0].scatter(data[:, 0], data[:, 1])
axes[1].scatter(data_rot[:, 0], data_rot[:, 1])
fig.show()
yields the image:
(whereas I expected a 45 counterclockwise rotation to make the data look like a vertical line) while changing the theta to np.pi / 2
yields the correct image of:
Your rotation matrix is correct. It's the auto scaling from matplotlib that is making look as though the rotation is wrong. Try adding these before fig.show()
axes[0].set_aspect("equal")
axes[1].set_aspect("equal")