[英]Plotting binned data with uneven bins
我有一個數據集,我已經設法將其分為 250 的間隔,但我很難正確繪制這些值。 我看過
但就我而言,我得到的只是一條垂直線。
供參考,我的分箱數據如下所示:
(0, 250] 2
(250, 500] 1
(500, 750] 5
(750, 1000] 13
(1000, 1250] 77
(1250, 1500] 601
(1500, 1750] 1348
(1750, 2000] 3262
(2000, 2250] 3008
(2250, 2500] 5118
(2500, 2750] 4576
(2750, 3000] 5143
(3000, 3250] 3509
(3250, 3500] 4390
(3500, 3750] 2749
(3750, 4000] 2794
(4000, 4250] 1391
(4250, 4500] 1753
(4500, 4750] 1099
(4750, 5000] 1592
(5000, 5250] 688
(5250, 5500] 993
(5500, 5750] 540
(5750, 6000] 937
(6000, 6250] 405
(6250, 6500] 572
(6500, 6750] 202
(6750, 7000] 369
(7000, 7250] 164
(7250, 7500] 231
...
(7750, 8000] 285
(8000, 8250] 55
(8250, 8500] 116
(8500, 8750] 29
(8750, 9000] 140
(9000, 9250] 31
(9250, 9500] 68
(9500, 9750] 20
(9750, 10000] 132
(10000, 10250] 15
(10250, 10500] 29
(10500, 10750] 21
(10750, 11000] 73
(11000, 11250] 26
(11250, 11500] 36
(11500, 11750] 21
(11750, 12000] 74
(12000, 12250] 5
(12250, 12500] 50
(12500, 12750] 13
(12750, 13000] 34
(13000, 13250] 4
(13250, 13500] 45
(13500, 13750] 14
(13750, 14000] 53
(14000, 14250] 6
(14250, 14500] 17
(14500, 14750] 7
(14750, 15000] 79
(15000, 10000000] 256
其中最后一個間隔包含大於 15,000 的所有內容。 我已將上述值放在一個list
然后嘗試繪制:
bins = [i for i in range(0, 15001, 250)]
bins.append(10000000)
categories = pd.cut(data["price"], bins)
price_binned = list(pd.value_counts(categories).reindex(categories.cat.categories))
plt.hist(price_binned)
它產生一個帶有 12 個 bin 的直方圖。 添加bin
參數
plt.hist(price_binned, bins=(bin_num+1))
產生一個直方圖,我在左邊得到一條非常高的垂直線。 最后,我正在考慮添加plt.xticks(bins)
,但后來我得到了一個沒有產生任何結果的圖表。
無論如何,我可以生成一個直方圖,其中 x 軸是 bin 值,y 軸是 bin 中的值?
使用plt.bar()
使用沒有 bin 參數的plt.hist()
使用帶有 bin=bins 的plt.hist()
使用seaborn
您遇到的主要問題似乎是您要求plt.hist()
和sns.distplot()
創建預裝箱直方圖數據的直方圖。
您可以使用條形圖通過price_binned
變量來促進您的自定義分箱方案,如下所示:
fig, ax = plt.subplots(1, 1)
ax.bar(range(len(bins)), price_binned, width=1, align='center')
ax.set_xticklabels([x + 125 for x in bins[:-1]])
plt.show()
我使用中點值作為每個 bin 的標簽。 這可以換成您喜歡的任何其他 xtick 標簽符號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.