接觸 numpy 遇到的第一個函數可能就是 linspace 函數,可是對於咱們這種沒有學過 matlab 的人來講,根本不知道這是什麼。python
因此只能本身查資料。
詞典顯示:less
線性等分向量 線性平分矢量 線性平分向量
那麼怎麼用呢?函數
linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) Return evenly spaced numbers over a specified interval. Returns `num` evenly spaced samples, calculated over the interval [`start`, `stop` ]. The endpoint of the interval can optionally be excluded. Parameters ---------- start : scalar The starting value of the sequence. 樣本起始值 stop : scalar The end value of the sequence, unless `endpoint` is set to False. 樣本終止值 In that case, the sequence consists of all but the last of ``num + 1`` evenly spaced samples, so that `stop` is excluded. Note that the step size changes when `endpoint` is False. num : int, optional 樣本個數 Number of samples to generate. Default is 50. endpoint : bool, optional If True, `stop` is the last sample. Otherwise, it is not included. Default is True. 若是爲真,則最後一個值(stop對應的值)包含在樣本中 retstep : bool, optional If True, return (`samples`, `step`), where `step` is the spacing between samples. 若是爲真,返回樣本及步長 dtype : dtype, optional The type of the output array. If `dtype` is not given, infer the data type from the other input arguments. 樣本的數據類型 .. versionadded:: 1.9.0 Returns ------- samples : ndarray There are `num` equally spaced samples in the closed interval ``[start, stop]`` or the half-open interval ``[start, stop)`` (depending on whether `endpoint` is True or False). step : float Only returned if `retstep` is True Size of spacing between samples. See Also -------- arange : Similar to `linspace`, but uses a step size (instead of the number of samples). logspace : Samples uniformly distributed in log space.
例:spa
import numpy as np import scipy as sp import pylab as pl # 從0到4pi之間,取100個樣點 x = np.linspace(0,4*np.pi,100) pl.plot(x,np.sin(x)) pl.show()
結果:
scala