獲取 Matplotlib 直方圖函式中的 bin 資訊


要獲取 matplotlib 直方圖函式中的 bin 資訊,我們可以採取以下步驟 -

  • 資料 和 bin 建立一個數字列表。

  • 使用直方圖()方法計算一組資料的直方圖。

  • 從直方圖(第 2 步)獲取直方圖 邊緣 

  • 找出直方圖中的頻率。

  • 使用箱 (步驟 1) 和頻率 (步驟 4) 資料製作一個條形圖。

  • 要顯示圖形,請使用show()方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
a = [-0.125, .15, 8.75, 72.5, -44.245, 88.45]
bins = np.arange(-180, 181, 20)
hist, edges = np.histogram(a, bins)
freq = hist/float(hist.sum())
plt.bar(bins[:-1], freq, width=20, align="edge", ec="k", color='red')
plt.show()

輸出

更新於: 2021-05-11

746 次瀏覽

開啟你的 職業生涯

透過完成課程獲取認證

開始學習
廣告
© . All rights reserved.