numpy.rot90() – The NumPy rot90 Python Function

Introduction to NumPy rot90

The numpy.rot90() function performs rotation of an array by 90 degrees in the plane specified by axis(0 or 1).

Syntax:

numpy.rot90(array, k = 1, axes = (0, 1))

Parameters:

  • array: [array_like] i.e. array having two or more dimensions.
  • k: [optional, int] No. of times we wish to rotate the array by 90 degrees.
  • axes: [array_like] Plane, along which we wish to rotate array.

Return: rotated copy of the array

Let’s have a look at some code snippets.

Code Examples of NumPy rot90

Example 01: Rotating Once

# welcome to softhunt.net
# Python Program illustrating
# numpy.rot90() method

import numpy as np

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

# Rotating once
print("\nArray being rotated 1 times : \n", np.rot90(array))

Output:

Original array : 
 [[0 1 2]
 [3 4 5]
 [6 7 8]]

Array being rotated 1 times : 
 [[2 5 8]
 [1 4 7]
 [0 3 6]]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

Example 02: Rotating Twice

# welcome to softhunt.net
# Python Program illustrating
# numpy.rot90() method

import numpy as np

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

# # Rotating twice
print("\nArray being rotated 2 times : \n", np.rot90(array, 2))

Output:

Original array : 
 [[0 1 2]
 [3 4 5]
 [6 7 8]]

Array being rotated 2 times : 
 [[8 7 6]
 [5 4 3]
 [2 1 0]]

Example 03: Rotating Three times

# welcome to softhunt.net
# Python Program illustrating
# numpy.rot90() method

import numpy as np

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

# # Rotating three times
print("\nArray being rotated 3 times : \n", np.rot90(array, 3))

Output:

Original array : 
 [[0 1 2]
 [3 4 5]
 [6 7 8]]

Array being rotated 3 times : 
 [[6 3 0]
 [7 4 1]
 [8 5 2]]

Example 04: Rotating four times

# welcome to softhunt.net
# Python Program illustrating
# numpy.rot90() method

import numpy as np

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


# # Rotating array 4 times : Returns same original array
print("\nArray being rotated 4 times : \n", np.rot90(array, 4))

Output:

Original array : 
 [[0 1 2]
 [3 4 5]
 [6 7 8]]

Array being rotated 4 times : 
 [[0 1 2]
 [3 4 5]
 [6 7 8]]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

How do I rotate a NumPy array 90 degrees?

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

How do you rotate an image in Python?

One of the Images transform operations that may be done to an Image is rotation. The image is rotated by a specified number of degrees around its center using Image rotation. 

Python Image Processing Library’s rotate() function. Pillow accepts the number of degrees as a parameter and rotates the image counterclockwise to the provided number of degrees.

Alternatively, one of the constants image.rotate_90, image.rotate_180, or image.rotate_270 can be use with the transpose function.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.roll() – The NumPy roll Python Function
  2. Python NumPy flip array Function

Leave a Comment