NumPy rot90 turns an array within a chosen pair of axes

A quarter turn rearranges array positions without calculating new numeric values. NumPy rot90 rotates an array in the plane defined by two axes and returns a rotated view. Academia Master Trader presents this English lesson for readers in Iraq, using constructed matrices to explain direction, shape changes and the importance of choosing the intended plane.

Begin with one counterclockwise quarter turn

After import numpy as np, define m = np.array([[2,4,6],[8,10,12]]). The call r = np.rot90(m) returns [[6,12],[4,10],[2,8]].

With the default axes (0,1) and k equal to one, the two dimensional display turns counterclockwise. The input shape (2,3) becomes (3,2). Six moves into the first position of the first row, while two moves into the first position of the last row. Follow particular entries to understand the rearrangement instead of reading the result as a transpose alone.

NumPy quarter turn

The parameter k counts quarter turns

The call np.rot90(m,k=2) returns [[12,10,8],[6,4,2]]. Two quarter turns make a half turn, and the shape is (2,3) again. This does not mean that the entries have returned to their starting positions.

The call np.rot90(m,k=-1) returns [[8,2],[10,4],[12,6]], a clockwise quarter turn with the default axes. Four quarter turns restore the original arrangement. An odd number of quarter turns exchanges the lengths of the selected axes, while an even number keeps those lengths in their original order.

Axis order determines the direction

The call np.rot90(m,axes=(1,0)) gives the same arrangement as np.rot90(m,k=-1). Reversing the order of the two axes reverses the rotation direction.

The axes must be different, and the input must have at least two dimensions. A vector has no two dimensional plane available for this function. Record both axes in their chosen order, together with k, so that another reader can reproduce the positional transformation without relying on a drawing.

A stack can rotate within each matrix

Define b = np.arange(1,13).reshape(2,2,3). To rotate each matrix while keeping the leading stack axis in place, use np.rot90(b,axes=(1,2)). The output shape is (2,3,2).

The first matrix [[1,2,3],[4,5,6]] becomes [[3,6],[2,5],[1,4]]. The default axes would instead select axes zero and one, which is a different plane. An array's printed layout cannot choose the intended plane for you. Match the axis numbers to the meaning of the data before applying the rotation.

The number of entries remains unchanged through these turns. A matrix with six positions still has six positions after rotation, even when its shape changes. This makes the example useful for separating size from shape: size counts entries, while shape records how those entries are arranged along each axis.

A rotated view shares the underlying data

In the original matrix example, assigning r[0,0] = 99 also changes m[0,2] to ninety nine. The rotation gives a different indexing arrangement over shared data. Use np.rot90(m).copy() when later edits need a separate array.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Keep the axis pair, direction, output shape and storage relationship beside any result you pass to another calculation.