按角度 theta 旋转数据

问题描述

可能是一个简单的问题,但假设您有想要逆时针旋转 90 度的二维法线数据,为此您可以使用 rotation matrices 并用 theta = np.pi / 2 构造一个,然后乘以旋转矩阵的数据。这很好用,但是当我尝试将数据旋转 45 度 (np.pi / 4) 时,它不起作用。它似乎顺时针旋转了数据,但翻转角度的符号不会改变结果图。如何将数据逆时针旋转 45 度?

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[:,data_rot[:,1])
fig.show()

产生图像:

enter image description here

(而我预计逆时针旋转 45 度以使数据看起来像一条垂直线)同时将 theta 更改为 np.pi / 2 会产生以下正确图像:

enter image description here

解决方法

您的旋转矩阵是正确的。这是来自 matplotlib 的自动缩放看起来好像旋转是错误的。尝试在 fig.show()

之前添加这些
axes[0].set_aspect("equal")
axes[1].set_aspect("equal")

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...