NumPy使用 Matplotlib 繪制直方圖


NumPy - 使用 Matplotlib 繪制直方圖

NumPy 有一個numpy.histogram()函數,它是數據的頻率分布的圖形表示。 水平尺寸相等的矩形對應於類間隔,稱為bin,變量height對應於頻率。

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 
Python

輸出如下:

[3 4 5 2 1] [0 20 40 60 80 100] 
Python

plt()

Matplotlib 可以將直方圖的數字表示轉換為圖形。 pyplot子模塊的plt()函數將包含數據和bin數組的數組作為參數,並轉換為直方圖。

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() 
Python

輸出如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM