Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

ndimage.rotate

Rotates an array in the plane defined by two axes using spline interpolation. The angle is given in degrees, with controls for output shape, interpolation, boundary behavior, and destination storage.

How it works

rotate applies spline interpolation to rotate an array within the plane defined by two selected axes. The angle is specified in degrees; optional settings control output shape, interpolation order, boundary handling, fill value, and prefiltering.

Syntax

scipy.ndimage.rotate(input, angle, axes=(1, 0), reshape=True, output=None, order=3, mode='constant', cval=0.0, prefilter=True)

Parameters

NameTypeDescription
inputarray_likeInput array.
anglefloatRotation angle in degrees.
axestuple of 2 ints, optionalTwo axis indices defining the rotation plane; defaults to the first two axes.
reshapebool, optionalWhether to adapt the output shape so the complete input fits; default is True.
outputarray or dtype, optionalDestination array or dtype of the returned array; by default a same-dtype array is created.
orderint, optionalSpline interpolation order, from 0 through 5; default is 3.
mode{'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}, optionalBoundary mode used when sampling outside the input.
cvalfloat, optionalValue used beyond the input boundary when the mode requires a constant value; default is 0.0.
prefilterbool, optionalWhether to prefilter the input for spline interpolation; default is True.

Returns

ndarray containing the rotated array; if output is supplied as an array, no separate array is returned.

Key points

  • Use ndimage.rotate to rotate an array.
  • The two integers in axes select the plane in which rotation occurs.
  • Interpolation is spline-based, and its order is controlled by order.
  • Pass the rotation amount as degrees through angle.
  • axes accepts a pair of axis indices and defaults to the first two axes.
  • output supports either a destination array or a dtype for the returned result.

Common mistakes

  • Keep order between 0 and 5; values outside that range are invalid.
  • With the default reshape=True, the result can have a different shape; use False when a fixed shape is required.
  • Unless output specifies a destination array or dtype, the returned array uses the input dtype.

Example: Rotate a 2D array

import numpy as np
from scipy import ndimage

image = np.arange(16).reshape(4, 4)
rotated = ndimage.rotate(image, 45, reshape=False, order=0)
print(rotated.shape)

Rotates a small 2D array by 45 degrees around its first two axes while retaining its original shape.

When to use it

Rotate a 2D image while choosing whether to preserve or expand its canvas.

Image processing

Rotate a slice or plane selected from a higher-dimensional array with `axes`.

Multidimensional array analysis

Write the result into an existing output array to control memory allocation.

Preallocated numerical workflows

Interview questions

QWhat does scipy.ndimage.rotate do, and which axes does it use by default?

It rotates the array in the plane described by axes, using spline interpolation with the requested order. The angle is measured in degrees, and the default axes are the first two axes.

Follow-up: How do you avoid changing the result dimensions? Set reshape=False; otherwise the output shape is adjusted to contain the rotated input completely.

QWhat is the purpose and valid range of order?

order selects the spline interpolation order and defaults to 3. The documented valid range is 0 through 5.

Follow-up: For nearest-neighbor-like behavior, choose order 0; for smoother interpolation, choose a higher supported order while considering the data type and application.

QHow does the output argument affect allocation and return behavior?

output can be an existing array receiving the result or a dtype used for the returned array. If an output array is supplied, the function does not return a separate result array.

Follow-up: When no destination is supplied, what determines the output dtype? By default, it is the same dtype as the input array.

  • scipy.ndimage.affine_transform
  • scipy.ndimage.zoom
  • scipy.ndimage.shift

Source: scipy.ndimage.rotate#. Checked 2026-10-15.