Arrays - linspace()
NumPy - The Basics
1 min read
This section is 1 min read, full guide is 14 min read
Published Sep 22 2025
11
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
NumPyPython
What np.linspace() Does
numpy.linspace()creates an array of evenly spaced numbers between a start and stop value.- Unlike
arange(), which depends on a step size,linspace()generates a specified number of points.
Function Signature
numpy.linspace(start, stop, num=50, endpoint=True, dtype=None)
Copy to Clipboard
- start → first value in the sequence.
- stop → last value (included by default).
- num → number of samples (default = 50).
- endpoint → if
True, includes the stop value (default). IfFalse, excludes it. - dtype → optional data type (e.g.,
int,float).
Examples
import numpy as np
# Default: 50 points from 0 to 1
np.linspace(0, 1)
# → array of 50 evenly spaced values between 0 and 1
# 5 points from 0 to 10
np.linspace(0, 10, 5)
# → [ 0. 2.5 5. 7.5 10. ]
# 5 points, excluding the stop value
np.linspace(0, 10, 5, endpoint=False)
# → [0. 2. 4. 6. 8.]
# Negative to positive range
np.linspace(-5, 5, 11)
# → [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]
# From -2.5 to 2.5, 6 points
np.linspace(-2.5, 2.5, 6)
# → [-2.5 -1.5 -0.5 0.5 1.5 2.5]
# With float precision and custom dtype
np.linspace(-1, 1, 5, dtype=np.float32)
# → [-1. -0.5 0. 0.5 1. ]
Copy to Clipboard
Toggle show comments
Key Notes
- Great for generating evenly spaced values when you know how many points you need, not the step size.
- Very useful in plotting (e.g., generating x-values for smooth curves).
- Preferable to
arange()for floating-point ranges, since it avoids precision issues.














