Numpy rot90() method rotates an array by 90 degrees clockwise or counterclockwise in a plane defined by two axes. You can rotate single or multiple times, depending on your requirement.
import numpy as np
input_arr = np.array([[5, 7, 9],
[11, 14, 15]])
rotated_arr = np.rot90(input_arr)
print(rotated_arr)
# Output:
# [[ 9 15]
# [ 7 14]
# [ 5 11]]
In the above code, we rotate the array counterclockwise by 90 degrees in the plane of axes (0, 1).
The rows become columns, and the first row becomes the first column, reversed.
The 5-7-9 row becomes the first 9-7-5 column, in reverse.
The 11-14-15 row becomes the last 15-14-11 column, in reverse.
You can see that the output is an array, but rotated.
Syntax
numpy.rot90(m, k=1, axes=(0, 1))
Parameters
| Argument | Description |
| m (array_like) | It is an array of at least two dimensions. |
| k (Integer, optional (default=1)) |
It is the number of times the array is rotated by 90 degrees. |
| axes (Tuple, optional (default=(0, 1)) | It represents the plane of rotation. It must be valid for the input array that needs to be rotated. |
Clock-wise rotation
To rotate an array counterclockwise, pass k = -1.
import numpy as np
input_arr = np.array([[5, 7, 9],
[11, 14, 15]])
clockwise_rotated_arr = np.rot90(input_arr, k=-1)
print(clockwise_rotated_arr)
# Output:
# [11 5]
# [14 7]
# [15 9]]
In this case, the last row becomes the first column, and the first row becomes the last column.
The last row 11->14->15 becomes the first column 11->14->15 (From top to bottom).
The first row 5->7->9 becomes the last column 5-7-9 (From top to bottom).
Multiple rotations
If we want to rotate two times, we can pass k=2, which will rotate 90* + 90* = 180*.
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
rotated_180_array = np.rot90(arr, k=2)
print(rotated_180_array)
# Output:
# [[6 5 4]
# [3 2 1]]
On the first rotation (90* counter-clockwise), we would have the first row 3-6, the second row 2-5, and the third row 1-4.
On the second rotation (90* counter-clockwise), we have the final output. The first row is 6-5-4, and the second row is 3-2-1. The array is flipped both horizontally and vertically.
ValueError: Axes must be different
If the input array is 1D and you try to rotate it by 90* using np.rot90(), it will throw an exception: ValueError: Axes must be different.
import numpy as np arr_1d = np.array([1, 2, 3]) result = np.rot90(arr_1d) print(result) # Output: ValueError: Axes must be different.
To prevent this type of error, ensure that the input array has two or more dimensions.
If the axes are the same as (0, 0), then it also raises the same error even if the input array is 2D or higher dimensions.
import numpy as np arr_2d = np.array([[1, 2], [3, 4]]) result = np.rot90(arr_2d, axes=(0, 0)) print(result) # Output: ValueError: Axes must be different.
That’s all!



