NumPy rot90 turns an array through quarter turns within a specified plane

An array rotation changes where entries appear in a grid. NumPy rot90 performs quarter turns in a plane defined by two axes. Academia Master Trader explains this operation in English for readers in Monaco, using a small rectangular matrix. The examples distinguish rotation from sorting, numerical calculation and a simple row exchange. Following the corners makes the direction visible and helps explain why a rectangular array can change its shape.

Read the original matrix before turning it

After import numpy as np, define a = np.array([[1,2,3],[4,5,6]]). It has two rows and three columns. The top row is one, two, three, and the bottom row is four, five, six. Its four corners are one, three, four and six.

The call np.rot90(a) returns [[3,6],[2,5],[1,4]]. With the default settings, a two dimensional matrix turns counterclockwise by ninety degrees. Three becomes the top left entry, six becomes the top right entry and one becomes the bottom left entry. The result has three rows and two columns.

NumPy rot90

Count quarter turns with k

The parameter k is the number of quarter turns. The call np.rot90(a,k=2) returns [[6,5,4],[3,2,1]]. Two quarter turns give a half turn, returning the shape to two rows by three columns. Both the row order and the entry order within each row are reversed in this two dimensional example.

The call np.rot90(a,k=4) restores the original arrangement. Four quarter turns complete a full turn. These comparisons make k easier to interpret than a general phrase such as rotating repeatedly. The parameter counts ninety degree steps, not individual positions within a row or column.

A negative count turns in the opposite direction

The call np.rot90(a,k=-1) returns [[4,1],[5,2],[6,3]]. Under the default axis order, this is a clockwise quarter turn. Four moves to the top left corner and three moves to the bottom right corner. The output again has three rows and two columns.

A positive quarter turn and a negative quarter turn therefore have the same output shape but different arrangements. Shape alone does not establish direction. Trace a named corner from the input to the output, then check a second corner to confirm which way the matrix turned.

Axis order defines the orientation of the plane

The default axes are (0,1). The call np.rot90(a,k=1,axes=(1,0)) produces the same arrangement as np.rot90(a,k=-1,axes=(0,1)). Reversing the order of the two axes reverses the rotation direction for the same positive count.

The two axes must be different, and the input must have at least two dimensions. For an array with more than two dimensions, the axes specify the plane in which the turn occurs. Write both axes in order when describing such an operation. Naming only the two dimensions without their order leaves the direction unclear.

A rotated view is not necessarily an independent copy

NumPy describes the result as a rotated view. For a NumPy array, that means the output can refer to the original entries rather than storing an independent collection of values. If later work requires a separate array, request a copy explicitly, such as np.rot90(a).copy().

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input shape, ordered axis pair and quarter turn count. Then trace the corners and check the resulting shape. A rotation rearranges the supplied entries; it does not generate additional measurements or make a numerical pattern more predictive.