Skip to content
LearnPY
NumPyfunction reference

np.linspace

Create a chosen number of evenly spaced points.

Signature

np.linspace(start, stop, num=50, endpoint=True)

What it does

np.linspace divides the interval from start to stop into num points. Unlike arange, you specify the count rather than the step, and the endpoint is included by default.

Parameters

startnumber
The first value in the sequence.
stopnumber
The final value when endpoint is True.
numint, optionaloptional
Number of points to generate. Defaults to 50.
endpointbool, optionaloptional
Whether to include stop in the result.

Returns

ndarray

Example

pythonnp.linspace example
import numpy as np

print(np.linspace(0, 1, 5))

Output

[0.   0.25 0.5  0.75 1.  ]

Watch out for

  • num must be a nonnegative integer; zero returns an empty array.
  • For integer grids with a natural step, arange may read more clearly.