2017-02-13 68 views
1

我有一個數據集,我已經設置了250個分區的間隔,並且我有一個非常困難的時間恰當地繪製值。我看了一下使用不均勻分箱繪製分箱數據

python plot simple histogram given binned data

How to make a histogram from a list of data

但對我來說我得到的是一個單一的垂直線。

參考我的離散化的數據是這樣的:

(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參數

plt.hist(price_binned, bins=(bin_num+1)) 

產生直方圖從哪裏獲得左側非常高的垂直線。最後,我正在考慮添加plt.xticks(bins),但是然後我得到一個什麼都不產生的圖。

無論如何,我可以生成一個直方圖,其中X軸是箱值,Y軸是箱中的值?

using <code>plt.bar()</code>

使用plt.hist()使用plt.hist()與塊沒有倉參數

using <code>plt.hist()</code> with bin=bins

使用plt.bar()

using <code>plt.hist()</code> with no bin argument

=倉

using seaborn

使用seaborn

+0

您顯示的數據已經是直方圖。因此,你很難清楚你的目標是什麼。 – ImportanceOfBeingErnest

+0

我想要一個可視化的數據表示,而不是隻將它作爲一個列表 – Lukasz

+0

當然,但是視覺表示中應該不同於已有的直方圖? – ImportanceOfBeingErnest

回答

2

你的主要問題似乎是,你問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() 

當我用中點值作爲標籤每個箱。這可以換成你喜歡的任何其他xtick標籤符號。

這是我使用(大部分)您的數據(有些丟失)的結果: result

+0

@Lukasz如果此答案解決了您的問題,請考慮[接受它作爲解決方案](http://stackoverflow.com/help/someone-answers)。 – Brian