1. 程式人生 > 實用技巧 >matplotlib--> pyplot&np.arange()函式的簡單用法!

matplotlib--> pyplot&np.arange()函式的簡單用法!

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 
 4 # Compute the x and y coordinates for points on sine and cosine curves
 5 x = np.arange(0, 3 * np.pi, 0.1)
 6 y_sin = np.sin(x)
 7 y_cos = np.cos(x)
 8 
 9 # Plot the points using matplotlib
10 plt.plot(x, y_sin)
11 plt.plot(x, y_cos)
12 plt.xlabel('
x axis label') 13 plt.ylabel('y axis label') 14 plt.title('Sine and Cosine') 15 plt.legend(['Sine', 'Cosine']) 16 plt.show()

np.arange()用法

返回值: np.arange()函式返回一個有終點和起點的固定步長的排列。

引數:

1)一個引數時,引數值為終點,起點取預設值0,步長取預設值1。

2)兩個引數時,第一個引數為起點,第二個引數為終點,步長取預設值1。

3)三個引數時,第一個引數為起點,第二個引數為終點,第三個引數為步長。其中步長支援小數。

例子:

a = np.arange(3) # 預設起點0,步長為1 輸出:[0 1 2]

a = np.arange(3,6) # 預設步長為1 輸出:[3 4 5]

a = np.arange(0, 3, 0.1) # 輸出:[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9]