NumPy rot90 turns an array through quarter rotations in a plane defined by two axes

A rectangular array can be turned without changing the numbers it contains. After a quarter rotation, however, a row can become a column and the lengths of those two dimensions can exchange places. NumPy rot90 describes this operation with a count of quarter turns and an ordered pair of axes.

Academia Master Trader presents this explanation in English for readers in Tanzania. The examples concern the arrangement of numerical data, not prices, trading signals or financial results. Understanding the position changes first makes it easier to distinguish a rotation from a transpose, a reversal or a cyclic shift.

Start with a rectangle rather than a square

Let x be a two-dimensional array with rows [1,2,3] and [4,5,6]. Its shape is (2,3): two rows and three columns. The expression np.rot90(x) returns an array with rows [3,6], [2,5] and [1,4]. Its shape is (3,2).

The default operation turns the array through ninety degrees counterclockwise in the plane of axes zero and one. The original upper-right value, three, becomes the upper-left value. Six becomes the upper-right value. Following a distinctive corner is a useful way to understand the direction without relying only on the outline of the rectangle.

Original diagram: rows one two three and four five six rotate counterclockwise into rows three six, two five and one four.

The six values are all still present, but their coordinates have changed. The shape changes because a quarter turn exchanges the two side lengths of this non-square rectangle. Using a square example alone could hide that consequence, since exchanging equal lengths leaves the shape looking the same.

The parameter counts quarter turns

The parameter k specifies how many ninety-degree rotations to apply. The default is one. For the same rectangle, np.rot90(x,2) gives rows [6,5,4] and [3,2,1]. That is a half turn, and its shape returns to (2,3).

With k equal to three, the result has rows [4,1], [5,2] and [6,3], with shape (3,2). Four quarter turns return the original arrangement. A zero count also leaves the arrangement unchanged. These outcomes describe position changes; they do not add ninety, two or four to the array values.

A negative count reverses the turn direction. With the default axes, np.rot90(x,-1) makes one clockwise quarter turn and gives the same arrangement as three counterclockwise quarter turns. Writing the direction alongside the expression helps a reader connect the number to the intended geometry.

The order of the axes matters

Rot90 takes an ordered pair of different axes. The rotation direction is from the first axis towards the second. For a two-dimensional array with the default axes (0,1) and k equal to one, the familiar visual result is counterclockwise.

Changing the pair to (1,0) reverses that direction for the same positive count. Thus np.rot90(x,1,axes=(1,0)) is equivalent in arrangement to np.rot90(x,-1,axes=(0,1)). The pair is not merely an unordered list of dimensions that happen to participate.

The two axes must be different, and the input needs at least two dimensions. A vector has only one axis, so the operation requires a plane that the vector alone does not provide. If a sequence is meant to represent a row or column within a plane, that shape should be made explicit before describing a rotation.

Distinguish a turn from a transpose

For the rectangle in this lesson, the transpose has rows [1,4], [2,5] and [3,6]. The default quarter rotation instead has rows [3,6], [2,5] and [1,4]. Both shapes are (3,2), but the arrays are not arranged the same way.

A shape comparison therefore cannot establish which transformation occurred. Transposition exchanges the two axes without the same direction reversal that appears in a quarter rotation. Comparing the positions of one, three, four and six makes the difference visible immediately.

Nor is a rotation the same as rolling values along a sequence. A cyclic shift returns entries that pass an end to the opposite end of a chosen dimension. A quarter rotation changes how positions relate across a plane. Their names should describe the operation actually intended by the program.

Higher dimensions still need a chosen plane

An array with more than two dimensions has several possible pairs of axes. Rot90 can act in the plane named by one such pair. The remaining dimensions are not replaced with new axes; the selected plane determines how the existing entries are rearranged.

Before interpreting the result, label what each dimension means. If an array has separate dimensions for several collections, row positions and column positions, the axes selected for rotation should match the intended plane. A visual description based on a two-dimensional page is only a guide when the actual array has more dimensions.

The official NumPy reference describes the result as a rotated view. Code should not assume that the operation provides independent storage for every value. When a separate array is required for later changes, make that intention explicit rather than inferring it from the new orientation.

Describe coordinates as well as appearance

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. Record the starting shape, the ordered axes and the quarter-turn count, then follow a corner value to its new position. That short explanation keeps the direction, the geometry and the numerical contents separate.