AppDividend
Latest Code Tutorials

Python NumPy rot90() Function Example

Python NumPy rot90() is an inbuilt function that is used for rotating the elements of an array by 90 degrees along a specified axis. The rot90() can be imported as import NumPy as np, and we can create multidimensional arrays and derive other mathematical statistics with the help of NumPy, which is a library in Python.

Python NumPy rot90()

The rot90() function is used to rotate the array by 90 degrees in the plane specified by axes. The rotation direction is from the first towards a second axis. An array of two or more dimensions. The number of times an array is rotated by 90 degrees.

Syntax

```numpy.rot90 (input_array, k = 1, axes = (0, 1))
```

Parameters

1. Input_array: It depicts the n-dimensional array where rotation is to be performed.
2. k: It represents the number of times we wish to rotate array by 90 degrees.
3. axes: It depicts the plane along which we want to rotate the array.

Return Value

It returns the rotated version of the input_array.

Write a program to show the working of the rot90() function.

See the following code.

```import numpy as np

array = np.arange(12).reshape(3, 4)
print("Original array : \n", array)

# Rotating once
print("\nRotated array : \n", np.rot90(array))

# Rotating twice
print("\nRotated array : \n", np.rot90(array, 2))```

Output

```Original array :
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

Rotated array :
[[ 3  7 11]
[ 2  6 10]
[ 1  5  9]
[ 0  4  8]]

Rotated array :
[[11 10  9  8]
[ 7  6  5  4]
[ 3  2  1  0]]```

Explanation

In the above code, Function 1 was used to rotate the array in 90 degrees only one time.

Function 2 was used to rotate the array in 90 degrees two times.

See the second example.

```import numpy as np

m = np.arange(8).reshape((2, 2, 2))
print("Original Array\n")
print(m)
print("Rotated Array\n")
print(np.rot90(m, 1, (1, 2)))
```

Output

```Original Array

[[[0 1]
[2 3]]

[[4 5]
[6 7]]]
Rotated Array

[[[1 3]
[0 2]]

[[5 7]
[4 6]]]```

Explanation

In the above code, function rotated the input array along (1, 2) place only one time.

Note: The numpy.rot90() returns the view. Because a view shares memory with an original array, changing one value changes the other.

Specify the number of times to rotate: k

Specifying the integer value for a second argument k rotates the array 90 degrees counterclockwise k times. See the following code.

```import numpy as np

m = np.arange(8).reshape((2, 2, 2))
print("Original Array\n")
print(m)
print("Rotated Array\n")
print(np.rot90(m, 10))

print("Rotated Array 2nd time\n")
print(np.rot90(m, 11))

print("Rotated Array 3rd time\n")
print(np.rot90(m, 21))```

Output

```Original Array

[[[0 1]
[2 3]]

[[4 5]
[6 7]]]
Rotated Array

[[[6 7]
[4 5]]

[[2 3]
[0 1]]]
Rotated Array 2nd time

[[[4 5]
[0 1]]

[[6 7]
[2 3]]]
Rotated Array 3rd time

[[[2 3]
[6 7]]

[[0 1]
[4 5]]]```

Conclusion

NumPy rot90() function is used to rotate the array by 90 degrees in the plane specified by axes. The rotation direction is from the first towards the second axis.