NumPy 有一個numpy.histogram()
函數,它是數據的頻率分佈的圖形表示。 水平尺寸相等的矩形對應於類間隔,稱爲bin
,變量height
對應於頻率。python
numpy.histogram()
numpy.histogram()
函數將輸入數組和bin
做爲兩個參數。 bin
數組中的連續元素用做每一個bin
的邊界。數組
import numpy as np a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) ] np.histogram(a,bins = [0,20,40,60,80,100]) hist,bins = np.histogram(a,bins = [0,20,40,60,80,100]) print hist print bins
輸出以下:函數
[3 4 5 2 1] [0 20 40 60 80 100]
plt()
Matplotlib 能夠將直方圖的數字表示轉換爲圖形。 pyplot
子模塊的plt()
函數將包含數據和bin
數組的數組做爲參數,並轉換爲直方圖。spa
from matplotlib import pyplot as plt import numpy as np a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) plt.hist(a, bins = [0,20,40,60,80,100]) plt.title("histogram") plt.show()
輸出以下:code