NumPy rot90 rearranges array entries through quarter turns in a selected plane

A quarter turn changes how array positions line up across two axes. NumPy rot90 performs these turns without calculating new values between the original entries. Academia Master Trader presents this English lesson for readers in Djibouti, using constructed arrays to explain rotation direction, output shape and the two axes defining a rotation plane.

The default rotates a matrix counterclockwise

After import numpy as np, define m = np.array([[1,2,3],[4,5,6]]). The call np.rot90(m) returns [[3,6],[2,5],[1,4]]. With the default axis order and one quarter turn, the matrix rotates counterclockwise.

The original top row becomes the first column, read from bottom to top. The final input column becomes the first output row. Following a distinctive entry, such as one or six, makes this positional movement easier to explain than relying on shape alone.

The input shape is (2,3), while the output shape is (3,2). A quarter turn exchanges the lengths of the two selected axes. No entries are added, removed or averaged during that rearrangement.

NumPy quarter turn

The k argument counts quarter turns

The call np.rot90(m,k=2) returns [[6,5,4],[3,2,1]]. Two quarter turns make a half turn, leaving this matrix with its original shape (2,3) but a different arrangement.

The call np.rot90(m,k=-1) returns [[4,1],[5,2],[6,3]]. Under the default axes, a negative quarter turn rotates clockwise. Four quarter turns return the original ordering, so k equal to four produces the same arrangement as k equal to zero.

Keep direction attached to the axis order. Saying that positive k always means counterclockwise is incomplete for a general array, because changing the order of the selected axes changes the direction represented by the same k.

The axis pair defines the plane and its direction

The default pair is (0,1). The call np.rot90(m,k=1,axes=(1,0)) returns [[4,1],[5,2],[6,3]], matching a negative quarter turn with the default pair.

For an array with more than two dimensions, choose the two distinct axes that define the intended plane. The remaining axes are not exchanged by that rotation. Naming both axes is useful when separate layers or channels must retain their role while another pair of dimensions rotates.

This operation is a positional rearrangement on a grid. It does not rotate a picture through an arbitrary angle or estimate intermediate pixel values. Quarter turns fit the existing grid, which is why every output entry can be traced directly to an input entry.

A rotated view can share data with the source

Rot90 accepts arrays with at least two dimensions. A one-dimensional vector does not provide the required two-axis plane. Check the dimensional structure before choosing an axis pair.

For an existing NumPy array, the returned result is a rotated view. It can share its underlying data with the source, so later edits through the view can affect the original array. Use np.rot90(m).copy() when the rotated result needs independent storage.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record k, the ordered axis pair and the expected output shape before interpreting a quarter turn.