📜  Python中的numpy.linspace

📅  最后修改于: 2020-06-03 01:05:42             🧑  作者: Mango

numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None) : 间隔均匀返回数字空间。与arange类似,但不使用step而是使用样本编号。

参数:

->start:[可选]间隔范围的开始。默认情况下开始= 0
->stop:间隔范围的结尾
-> restep:如果为True,则返回(样本,步进)。通过退潮步=假
-> num:[int,可选]要生成的样本数
-> dtype:输出数组的类型

返回:

ndarray

代码1:解释linspace函数

# Python编程说明numpy.linspace方法 
  
import numpy as geek 
  
# 重新设置为True 
print("B\n", geek.linspace(2.0, 3.0, num=5, retstep=True), "\n") 
  
# 远程评估sin()  
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模块– pylab的numpy.linspace()的图形表示 

# 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()的图形表示

# 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)

输出: