Arrays - linspace()

NumPy - The Basics

1 min read

Published Sep 22 2025, updated Aug 17 2026


11
0
0
0

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)

  • 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). If False, excludes it.
  • dtype → optional data type (e.g., int, float).



Examples

import numpy as np# Default: 50 points from 0 to 1np.linspace(0, 1)# → array of 50 evenly spaced values between 0 and 1# 5 points from 0 to 10np.linspace(0, 10, 5)# → [ 0.   2.5  5.   7.5 10. ]# 5 points, excluding the stop valuenp.linspace(0, 10, 5, endpoint=False)# → [0. 2. 4. 6. 8.]# Negative to positive rangenp.linspace(-5, 5, 11)# → [-5. -4. -3. -2. -1.  0.  1.  2.  3.  4.  5.]# From -2.5 to 2.5, 6 pointsnp.linspace(-2.5, 2.5, 6)# → [-2.5 -1.5 -0.5  0.5  1.5  2.5]# With float precision and custom dtypenp.linspace(-1, 1, 5, dtype=np.float32)# → [-1.  -0.5  0.   0.5  1. ]




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.
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact
NumPy - The Basics | Arrays - linspace() | SimpleSteps.guide