2 回答

TA贡献1858条经验 获得超8个赞
好吧,无论您是围绕右下角、左上角还是任何其他点旋转数组都没有关系:所有这些都只是旋转。唯一更重要的方面是您希望阵列旋转的方向,顺时针或逆时针,实际上,您可以使用另一个:顺时针旋转 90 度与旋转 270 度相同在相反的方向。
如果要顺时针旋转数组 90 度,请使用np.rot90,但告诉numpy将数组旋转 3 次,如果要逆时针旋转数组 90 度,请使用np.rot90传入1或不传入(当然除了数组):
np.rot90(array) # 90 deg CCW
np.rot90(array, 1) # 90 deg CCW
np.rot90(array, 3) # 90 deg CW
例子
import numpy as np
array = np.arange(15).reshape((3, 5))
print(array)
print(np.rot90(array, 1))
print(np.rot90(array, 3))
原始数组
array([
[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]
])
旋转 1 次(逆时针 90 度)
array([
[ 4, 9, 14],
[ 3, 8, 13],
[ 2, 7, 12],
[ 1, 6, 11],
[ 0, 5, 10]
])
旋转 3 次(顺时针 90 度)
array([
[10, 5, 0],
[11, 6, 1],
[12, 7, 2],
[13, 8, 3],
[14, 9, 4]
])

TA贡献2021条经验 获得超8个赞
使用 np.swapaxes():
>>> x = np.array([[1,2,3]])
>>> np.swapaxes(x,0,1)
array([[1],
[2],
[3]])
添加回答
举报