当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python numpy.linspace()用法及代码示例


关于:
numpy.linspace(开始,停止,num = 50,端点= True,retstep = False,dtype = None):以间隔间隔均匀地返回数字空间。与arange类似,但不使用step而是使用样本编号。
参数:

-> start  : [optional] start of interval range. By default start = 0
-> stop   : end of interval range
-> restep : If True, return (samples, step). By deflut restep = False
-> num    : [int, optional] No. of samples to generate
-> dtype  : type of output array

返回:

-> ndarray
-> step : [float, optional], if restep = True

代码1:解释linspace函数


# Python Programming illustrating 
# numpy.linspace method 
  
import numpy as geek 
  
# restep set to True 
print("B\n", geek.linspace(2.0, 3.0, num=5, retstep=True), "\n") 
  
# To evaluate sin() in long range  
x = geek.linspace(0, 2, 10) 
print("A\n", geek.sin(x))

输出:

B
 (array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)

A
 [ 0.          0.22039774  0.42995636  0.6183698   0.77637192  0.8961922
  0.9719379   0.99988386  0.9786557   0.90929743]

代码2:使用matplotlib模块的numpy.linspace()的图形表示-pylab

# Graphical Represenation of numpy.linspace() 
import numpy as geek 
import pylab as p 
  
# Start = 0 
# End = 2 
# Samples to generate = 10 
x1 = geek.linspace(0, 2, 10, endpoint = False) 
y1 = geek.ones(10) 
  
p.plot(x1, y1, '*') 
p.xlim(-0.2, 1.8)

输出:

代码3:使用pylab的numpy.linspace()的图形表示

# Graphical Represenation of numpy.linspace() 
import numpy as geek 
import pylab as p 
  
# Start = 0 
# End = 2 
# Samples to generate = 15 
x1 = geek.linspace(0, 2, 15, endpoint = True) 
y1 = geek.zeros(15) 
  
p.plot(x1, y1, 'o') 
p.xlim(-0.2, 2.1)

输出:

注意:
这些NumPy-Python程序不会在onlineID上运行,因此请在您的系统上运行它们以进行探索



相关用法


注:本文由纯净天空筛选整理自 numpy.linspace() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。