Arrays - linspace()

NumPy - The Basics

1 min read

Published Sep 22 2025


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 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. ]




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.

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact