Linspace

Date: March 2nd 2016
Last updated: March 2nd 2016

numpy.linspace is often seen in examples of plotting. From the docs, "numpy.linspace returns evenly spaced numbers over a specified interval." See http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.linspace.html.

Default start/stop creates 50 numbers

import numpy as np

# length of default array
len(np.linspace(0,10.0))
# 50

# Produce default numbers from 1 to 10
np.linspace(0,10.0)
#array([  0.        ,   0.20408163,   0.40816327,   0.6122449 ,
#         0.81632653,   1.02040816,   1.2244898 ,   1.42857143,
#         1.63265306,   1.83673469,   2.04081633,   2.24489796,
#         ... <snipped> ...
#         8.16326531,   8.36734694,   8.57142857,   8.7755102 ,
#         8.97959184,   9.18367347,   9.3877551 ,   9.59183673,
#         9.79591837,  10.        ])

Reduce default numbers

np.linspace(0,10.0, num=5)
#array([0., 2.5, 5., 7.5, 10.])

Get the step size between numbers

np.linspace(0,10.0, num=5, retstep=True)
#(array([  0. ,   2.5,   5. ,   7.5,  10. ]), 2.5)

Don't include the last number

np.linspace(0, 10.0, endpoint=False, num=5, retstep=True)
#(array([ 0.,  2.,  4.,  6.,  8.]), 2.0)

Examples
Creating data for plotting a normal distribution

results matching ""

    No results matching ""