NumPy Linspace Function in Python: Step-by-Step

Claudio Sabato
6 min readNov 12, 2022

Would you like to know how to use the Numpy linspace function in your Python programs? We will go through that together.

Many technical fields such as engineering, finance, and mathematics frequently deal with evenly-spaced numbers. NumPy Linspace allows the generation of evenly-spaced numbers using Python. You can also use linspace together with Matplotlib to plot complex mathematical functions.

In this tutorial, you will understand the NumPy Linspace function!

How Does Linspace Work in NumPy?

A sequence of numbers where the difference between each consecutive element is equal is called an evenly-spaced sequence of numbers.

NumPy Linspace allows you to create evenly-spaced numbers very easily and lets you customize parameters such as the endpoint of the range, the dtype of the resulting array, and the axis if you want to work with array-like objects.

First, let’s create evenly-spaced numbers with Numpy Linspace. Numpy Linspace requires the start and the stop points of the sequence you want to create.

import numpy as np

simple_space = np.linspace(start=2, stop=10)
print(simple_space)

The output is:

[ 2.          2.16326531  2.32653061  2.48979592  2.65306122  2.81632653
2.97959184 3.14285714 3.30612245 3.46938776 3.63265306 3.79591837
3.95918367 4.12244898 4.28571429 4.44897959 4.6122449 4.7755102
4.93877551 5.10204082 5.26530612 5.42857143 5.59183673 5.75510204
5.91836735 6.08163265 6.24489796 6.40816327 6.57142857 6.73469388
6.89795918 7.06122449 7.2244898 7.3877551 7.55102041 7.71428571
7.87755102 8.04081633 8.20408163 8.36734694 8.53061224 8.69387755
8.85714286 9.02040816 9.18367347 9.34693878 9.51020408 9.67346939
9.83673469 10. ]

The following print statement shows that the simple_space object we have created has 50 elements:

print(simple_space.shape)

[output]
(50,)

The notation (50,) represents a Python tuple.

But, what if we want to know what is the datatype of the object returned by numpy.linspace()?

Claudio Sabato

I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!