.linspace()
StevenSwiniarski474 total contributions
Published Jun 13, 2022
Contribute to Docs
The .linspace()
function returns an array of evenly-spaced numbers over a specified interval [start
,stop
], optionally excluding the stop
value.
Syntax
numpy.linspace(start, stop, num, endpoint, retstep, dtype, axis)
The start
and stop
arguments are required and represent the beginning and end of the interval. They can be numbers or arrays.
.linspace()
provides the following arguments:
start
: The starting point of the sequence.stop
: The (optionally included) endpoint of the sequence.num
: The number of values to generate. Defaults to 50.endpoint
: Boolean flag. IfTrue
,stop
is included as the last value. IfFalse
,stop
is excluded. Defaults toTrue
.retstep
: Boolean flag. IfTrue
, the result will include the calculated step size between values. Defaults toFalse
.dtype
: Thedtype
of the returned array, if omitted,dtype
is inferred fromstart
andstop
. Defaults toNone
.axis
: Ifstart
andstop
are arrays, this specifies on what axis the values will be added. If0
the axis is added at the beginning. If-1
, it’s added at the end. Defaults to0
.
Example
The following example creates a list of values between 10 and 20.
import numpyresult = numpy.linspace(10, 20, num=6)print(result)result2 = numpy.linspace(10, 20, num=6, endpoint=False)print(result2)result3 = numpy.linspace([1,2,3],[4,5,6], num=6)print(result3)
This results in the following output:
[10. 12. 14. 16. 18. 20.][10. 11.66666667 13.33333333 15. 16.66666667 18.33333333][[1. 2. 3. ][1.6 2.6 3.6][2.2 3.2 4.2][2.8 3.8 4.8][3.4 4.4 5.4][4. 5. 6. ]]
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.