np.linspace()
np.linspace() gives you an exact number of evenly spaced points between two values, including the end.
In this page:
Syntax
np.linspace(start, stop, num)
np.linspace()
np.linspace(start, stop, num) returns num evenly spaced samples and includes stop by default. It is the go-to tool for plotting axes and sampling functions. Set endpoint=False to exclude the last point.
Note:
Use retstep=True to also get the spacing between points.
Example: np.linspace()
import numpy as np
print(np.linspace(0, 1, 5))
print(np.linspace(0, 10, 4))
print(np.linspace(0, 1, 4, endpoint=False))
# Output:
# [0. 0.25 0.5 0.75 1. ]
# [ 0. 3.33333333 6.66666667 10. ]
# [0. 0.25 0.5 0.75]
Related Topics
Common Mistakes
- Confusing the third argument with a step size
- Forgetting the endpoint is included
- Using arange with floats when linspace is safer
Chapter Summary
- linspace takes a point count not a step
- Includes the endpoint by default
- Great for plotting ranges
- endpoint=False excludes the stop
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: