設定 Matplotlib 直方圖中的相對頻率


要設定 matplotlib 直方圖的相對頻率,我們可以採取以下步驟 -

  • 建立一個用於資料和柱的數字列表。

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

  • 從直方圖獲取 hist 和 edges

  • 找出直方圖的頻率。

  • 使用柱(步驟 1)和 freq 資料(步驟 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()

輸出

更新日期: 08-05-2021

1 千人次瀏覽

開始你的事業

透過完成課程獲取認證

開始
廣告