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

SciPy · Function

integrate.trapezoid

Numerically integrates sampled values with the composite trapezoidal rule. It can use explicit sample points or uniform spacing and integrates along a selected axis.

How it works

The function applies the composite trapezoidal rule to each one-dimensional slice of y along the selected axis. With x supplied, it uses the sample points in their given order; otherwise, it assumes uniform spacing specified by dx.

Syntax

scipy.integrate.trapezoid(y, x=None, dx=1.0, axis=-1)

Parameters

NameTypeDescription
yarray_likeInput samples to integrate.
xarray_like, optionalSample points corresponding to y; defaults to uniform spacing when omitted.
dxscalar, optionalSpacing between sample points when x is None; default 1.
axisint, optionalAxis along which to integrate; default is the last axis.

Returns

float for one-dimensional y; an n-1 dimensional array for higher-dimensional y.

Key points

  • Uses the composite trapezoidal rule along a chosen array axis.
  • The optional x argument supplies sample points aligned with y.
  • Without x, points are assumed to have uniform spacing given by dx.
  • axis chooses the dimension reduced by the integration.
  • One-dimensional input returns a scalar float.
  • For multidimensional input, the result has one fewer dimension.

Common mistakes

  • Sample points are consumed in their supplied order, so unsorted x values change the signed integral rather than being reordered.
  • Providing x also supports integration along a parametric curve, not only ordinary uniformly sampled functions.

Example: Integrate sampled values

import numpy as np
from scipy.integrate import trapezoid

x = np.linspace(0.0, np.pi, 1001)
area = trapezoid(np.sin(x), x=x)
print(area)  # approximately 2.0

Computes the area under sampled sine values over the interval from 0 to π.

When to use it

Sampled-curve integration

Estimate the area under experimentally or computationally sampled curves.

Array-wise reduction

Integrate arrays of signals or measurements along rows, columns, or another dimension.

Nonuniform-grid integration

Compute signed integrals for data whose sample coordinates are explicitly known.

Parametric-curve integration

Evaluate integrals represented by coordinates along a parametric path.

Interview questions

QWhat does scipy.integrate.trapezoid compute?

It computes a numerical integral with the composite trapezoidal rule along the requested axis. For one-dimensional y, the result is a float; higher-dimensional input produces one fewer dimension along that axis.

Follow-up: What happens when x is omitted? The samples are treated as evenly spaced, using dx as the spacing.

QHow do x and dx affect the calculation?

x contains sample points corresponding to y and determines the interval widths. If x is None, the function instead uses a constant spacing of dx, whose default is 1.

Follow-up: Does the function sort x first? No; it integrates in the sequence supplied by the caller.

QHow is multidimensional input handled?

axis selects the dimension over which integration occurs, and its default is the last axis, represented by -1. This lets the same function reduce rows, columns, or another dimension of an array.

Follow-up: What shape results from integrating an n-dimensional array? The selected axis is removed, so the result has one fewer dimension.

  • scipy.integrate.simpson
  • scipy.integrate.cumulative_trapezoid

Source: trapezoid — SciPy v1.18.0 Manual. Checked 2026-10-15.