# Numpy linspace() method In this article, we will be having a look at the `Numpy linspace()` function. Python NumPy module has got different functions to manipulate the arrays and perform operations on the elements in it.

## Getting Started with NumPy linspace()

NumPy’s numpy.linspace() function is basically used to generate a linear sequence out of the range of numbers provided.

Usually, `numpy.arange()` function can be used to generate sequences out of a range of numbers. The problem with numpy.arange() function is that it provides a loss of precision in the result if a floating type argument is provided to it.

Thus, numpy.linspace() function can be preferred over it.

Syntax:

```numpy.linspace(start, stop, num = value, endpoint = True/False, retstep = False/True, dtype = None)
```
• `start`: This value indicates the starting point of the sequence. Default value is considered as zero(0).
• `stop`: This value indicates the endpoint of the sequence.
• `num`: It indicates the number of steps or elements to be generated between the start and stop range.

Example:

```import numpy

inp = numpy.linspace(1, 3, 5)

print('Linear Sequence:\n',inp)
print('Length of the sequence:\n')
print(len(inp))
```

Output:

```Linear Sequence:
[1.  1.5 2.  2.5 3. ]
Length of the sequence:
5
```

### The retstep parameter of Numpy linspace()

The `retstep `parameter is basically a boolean value. If provided as True, it specifies the size of the steps taken between every element to generate the sequence. Then it results in the sequence as a tuple.

Example:

```import numpy

inp = numpy.linspace(1, 3, num=5, retstep=True)
print(inp)
print("Length of the sequence:",len(inp))
```

As mentioned above, when retstep = True is passed as an argument to the linspace() method, it generates a tuple as output. So the length of tuple would be 2, not 6!

Output:

```(array([1. , 1.5, 2. , 2.5, 3. ]), 0.5)
Length of the sequence: 2
```

### The endpoint parameter of linspace() method

The `endpoint` parameter is a boolean value. If set to False, it excludes the last number of the sequence in the result. The default value of the endpoint is True.

Example:

```import numpy

inp = numpy.linspace(1, 3, 5, endpoint=False)

print('Sequence from 1 to 3:', inp)

```

Output:

```Sequence from 1 to 3: [1.  1.4 1.8 2.2 2.6]
```

### The axis parameter of linspace() method

The `axis` parameter basically allows the user to provide an axis for the generated sequences to be stored along. The axis parameter can be applied only when the start and endpoint of data are of an array type.

Example:

```import numpy

inp1 = numpy.array([[10, 20, 30], [70, 40, 50]])
inp2 = numpy.array([[1, 3, 9], [5, 7, 11]])

op1 = numpy.linspace(inp1, inp2, 4, axis=0)
print(op1)

op2 = numpy.linspace(inp1, inp2, 2, axis=1)
print(op2)
```

When axis = 0, it takes the sequence limits from the first provided axis. The sub-array pairs `[10, 20, 30] and [1, 3, 9]` along with `[70, 40, 50] and [5, 7, 11]` are considered as limits to derive the sequence from inp1 to inp2.

When axis = 1, it uses column sequence to generate the elements from the given range.

Output:

```[[[10.         20.         30.        ]
[70.         40.         50.        ]]

[[ 7.         14.33333333 23.        ]
[48.33333333 29.         37.        ]]

[[ 4.          8.66666667 16.        ]
[26.66666667 18.         24.        ]]

[[ 1.          3.          9.        ]
[ 5.          7.         11.        ]]]

[[[10. 20. 30.]
[ 1.  3.  9.]]

[[70. 40. 50.]
[ 5.  7. 11.]]]
```

## NumPy linspace() function with Python matplotlib.pylab module

NumPy linspace() function can be understood by representing it with the help of pylab from the matplotlib library.

Example:

```import numpy
import pylab

inp1 = numpy.linspace(10, 20, 5, endpoint = False)
inp2 = numpy.linspace(10, 20, 5, endpoint = False)

pylab.plot(inp1, inp2, '*')
```

Output: