numpy.linspace使用详解

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

在指定的间隔内返回均匀间隔的数字。

返回num均匀分布的样本,在[start, stop]。

这个区间的端点可以任意的被排除在外。

1 arange
2 Similar to linspace, but uses a step size (instead of the number of samples).
3 arange使用的是步长,而不是样本的数量 
4 
5 logspace
6 Samples uniformly distributed in log space.

当endpoint被设置为False的时候
>>> import numpy as np
>>> np.linspace(1, 10, 10)
array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])
>>> np.linspace(1, 10, 10, endpoint = False)
array([ 1. ,  1.9,  2.8,  3.7,  4.6,  5.5,  6.4,  7.3,  8.2,  9.1])
In [4]: np.linspace(1, 10, 10, endpoint = False, retstep= True)
Out[4]: (array([ 1. ,  1.9,  2.8,  3.7,  4.6,  5.5,  6.4,  7.3,  8.2,  9.1]), 0.9)

1 >>> np.linspace(2.0, 3.0, num=5)
2     array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ])
3 >>> np.linspace(2.0, 3.0, num=5, endpoint=False)
4     array([ 2. ,  2.2,  2.4,  2.6,  2.8])
5 >>> np.linspace(2.0, 3.0, num=5, retstep=True)
6     (array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)
 1 >>> import matplotlib.pyplot as plt
 2 >>> N = 8
 3 >>> y = np.zeros(N)
 4 >>> x1 = np.linspace(0, 10, N, endpoint=True)
 5 >>> x2 = np.linspace(0, 10, N, endpoint=False)
 6 >>> plt.plot(x1, y, 'o')
 7 [<matplotlib.lines.Line2D object at 0x...>]
 8 >>> plt.plot(x2, y + 0.5, 'o')
 9 [<matplotlib.lines.Line2D object at 0x...>]
10 >>> plt.ylim([-0.5, 1])
11 (-0.5, 1)
12 >>> plt.show()

原文地址:https://www.cnblogs.com/peterwong666/p/11139135.html