[英]Matplotlib Histogram Bins Include/Exclude
bins
參數定義了 bin 的數量或 bin 的邊緣。 如果要創建具有n
bin 的直方圖,則定義 bin 邊緣的列表bins
的大小為n+1
。 所以這里我們希望 bin 邊緣為[9, 10, 11, 12]
(如果align='left'
; 或[8.5, 9.5, 10.5, 11.5]
如果align = 'mid'
)。
import matplotlib.pyplot as plt
import numpy as np
data = [9, 9, 11, 10, 9, 11, 11, 9]
width = 1
left, right = min(data), max(data) + width
# using 'bins' as the number of bins
nbins = int((right - left) / width)
plt.hist(data, range=(left, right), bins=nbins, align="left")
# defining the sequence of bin edges
plt.hist(data, bins=np.arange(left, right + width, width), align="left")
# and without the alignment parameter
plt.hist(data, bins=np.arange(left, right + width, width) - width / 2)
# if 'data' only contains integers and 'width=1'
plt.bar(*np.unique(data, return_counts=True))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.