這個問題在這里已經有了答案: Matplotlib histogram from numpy histogram output 2答案 3 小時前關閉。
我使用 numpy.histogram 創建了幾個直方圖,但在繪制它們時遇到了問題。我的代碼基本上是:
hist_data, bin_edges = np.histogram(pw_dist, 10, (0, limit 1), None, weights)
#perform some logic on histogram data
plt.hist(hist_data, bins=bin_edges)
hist_data 是 ([ 0., 0., 1176., 576., 2628., 1680., 2952., 3312., 3444., 2904.])
和 bin_edges 是: ([ 0. , 1.6, 3.2, 4.8, 6.4, 8. , 9.6, 11.2, 12.8, 14.4, 16. ])
這很好,但是,當嘗試使用 plt.hist 繪制它時,我得到的是: hist_data 和 bin_edges 上方的直方圖與 hist_data 陣列中的值并不真正對應。所以 TLDR,我如何使用 matplotlib.pyplot hist 函式來正確繪制直方圖,給定一個直方圖資料/箱陣列?
uj5u.com熱心網友回復:
plt.bar怎么樣?
hist_data, bin_edges = np.histogram(pw_dist, 10, (0, limit 1), None, weights)
# Plot the histogram
fig, ax = plt.subplots()
ax.bar(bin_edges[:-1], hist_data, width=np.diff(bin_edges))
請注意,我們通過獲取 中相鄰元素的差異來計算 bin 的寬度bin_edges。
matplotlib hist 函式將原始資料作為輸入,因此您也可以嘗試plt.hist(pw_dist)快速繪制直方圖。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/457241.html
標籤:Python 麻木的 matplotlib 直方图
