NumPy rot90 turns a rectangular array by a quarter turn with a defined direction

A quarter turn changes the positions of rows and columns together. It is different from reversing only one axis or exchanging row and column roles without the appropriate reversal. Academia Master Trader explains NumPy rot90 in English for readers in Albania. We will follow a small rectangular matrix through one counterclockwise turn, a clockwise turn and a half turn. Every output uses the original values. These examples concern array arrangements, not predictions, additional observations or evidence of financial performance.

Record the input before naming a direction

After import numpy as np, define a = np.array([[8,3,6],[2,9,4]]). Its shape is (2,3), with two rows and three columns. The first row contains eight, three and six. The second row contains two, nine and four. Recording this layout gives a fixed reference for words such as clockwise, counterclockwise, upper and lower.

The default call np.rot90(a) returns [[6,4],[3,9],[8,2]], with shape (3,2). This is one counterclockwise quarter turn in the default plane of axes (0,1). Original column two, containing six above four, becomes the first output row. Original column one becomes the second output row, and original column zero becomes the last output row.

Follow the corners through the turn

The original upper right entry six at (0,2) becomes the output upper left entry at (0,0). Original lower right entry four at (1,2) becomes output upper right at (0,1). Original upper left entry eight at (0,0) becomes output lower left at (2,0). These corner movements are consistent with a counterclockwise turn of the displayed matrix.

For this default two dimensional quarter turn, an original position (i,j) maps to (n-1-j,i), where n is the original number of columns. Here n equals three. The input position (1,1), containing nine, therefore maps to (1,1). That coincidence belongs to this particular rectangular shape and position; it does not mean that rotation leaves every middle entry at unchanged coordinates.

NumPy rot90

Read the figure as a change of layout

The figure puts the two by three input beside the three by two default result. Matching colours track the original first and last columns into the last and first output rows. Read those groups as complete pairs. Eight and two remain associated as a pair, but now appear next to each other rather than one above the other.

The figure is a positional explanation, not a plot with a measured horizontal or vertical scale. Every cell contains one original entry, and the number of cells stays six. The output shape changes because the rectangular layout turns, not because values were added or removed. A square example would keep the same shape after a quarter turn, making coordinate tracking even more useful.

Compare clockwise and half turns

The call np.rot90(a,k=-1) returns [[2,8],[9,3],[4,6]]. A negative one requests one quarter turn in the reverse direction, clockwise for the default axes. Its shape is also (3,2), so shape alone cannot tell clockwise from counterclockwise. The first row distinguishes them: two then eight for the clockwise result, six then four for the default counterclockwise result.

The call np.rot90(a,k=2) returns [[4,9,2],[6,3,8]], with shape (2,3). Two quarter turns make a half turn. Both the order of rows and the order within each row are reversed relative to the input. Four quarter turns return the original arrangement. The parameter k counts quarter turns; it is not a request to rotate by k degrees.

Keep the axes and shared data explicit

NumPy rot90 requires at least two dimensions, and the two chosen axes must be different. For a two dimensional input, changing axes=(0,1) to axes=(1,0) reverses the rotation direction. State both k and the axes when describing a result, especially for arrays with more than two dimensions, where the selected plane matters.

The documented result is a rotated view that shares data with its source. Use .copy() when an independent result is needed before later edits. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the original shape, name the direction and follow two corners. These checks distinguish a quarter turn from a transpose or a single axis reversal without changing the meaning of the supplied values.