2015-04-21 181 views
3

我有一個看起來像這樣的數據:熊貓堆疊barplot與分組酒吧

topic positive negative  type 
0  88 0.080000 0.030000 source 
1  36 0.010000 0.200000 source 
2 101 0.350000 0.040000 source 
3  78 0.110000 0.090000 source 
4  99 0.110000 0.010000 source 
5  79 0.000000 0.050000 source 
6  24 0.000000 0.160000 source 
7  17 0.000000 0.410000 source 
8  14 0.090000 0.050000 source 
9  29 0.060000 0.030000 source 
0  14 0.207071 0.085859 summary 
1  17 0.000000 0.738889 summary 
2  24 0.000000 0.219349 summary 
3  29 0.000000 0.094907 summary 
4  36 0.000000 0.255808 summary 
5  78 0.108333 0.194444 summary 
6  79 0.000000 0.106443 summary 
7  88 0.089286 0.041667 summary 
8  99 0.098496 0.050877 summary 
9 101 0.444444 0.055556 summary 

我需要繪製柱狀圖,對不同type每個topic比較正/負值。我看到它與x軸上的topic堆疊(正/負)條形圖一樣,並且條形使用type列進行分組。但我找不到一種方法來建立分組和堆積的條形圖。

對於這個樣子的單片式(抱歉,我沒有足夠的信譽發表圖片):

polar_data.set_index(['type', 'topic']).xs('summary').plot(kind='bar', stacked=True) 

我目前可以比較兩種不同類型的唯一途徑只有通過將兩個地塊側通過使用seaborn.factorplot,這不允許清楚地注意到趨勢。而且我也不知道如何用seaborn構建堆積條形圖。

print_data = pd.melt(polar_data, id_vars=['topic', 'type'], value_name='percent', var_name='polarity') 
sns.factorplot("topic", 'percent', 'polarity', row="type", data=print_data, margin_titles=True, kind='bar') 

所以,有什麼辦法可以並排地側的「合併」他們呢?

+0

我在這裏有點困惑。你打算如何比較正面和負面的價值? – Zero

+0

@JohnGalt讓我解釋一下。我想要這樣的情節[這裏](負值/正值)堆疊在彼此的頂部,並有兩個這樣的酒吧每個主題(一個是「摘要」類型,另一個是「源」類型),它們應該彼此靠近分組。所以每個主題都有一個組 – AAzza

回答

5

這裏有一種使用matplotlib的方法。我猜seaborn會使用相同的結構。

In [3]: polar_data.pivot('topic', 'type') 
Out[3]: 
     positive   negative 
type  source summary source summary 
topic 
14   0.09 0.207071  0.05 0.085859 
17   0.00 0.000000  0.41 0.738889 
24   0.00 0.000000  0.16 0.219349 
29   0.06 0.000000  0.03 0.094907 
36   0.01 0.000000  0.20 0.255808 
78   0.11 0.108333  0.09 0.194444 
79   0.00 0.000000  0.05 0.106443 
88   0.08 0.089286  0.03 0.041667 
99   0.11 0.098496  0.01 0.050877 
101  0.35 0.444444  0.04 0.055556 

所以,現在正值你可以做 -

polar_data.pivot('topic', 'type')['positive'].plot(kind='bar', stacked=True) 

enter image description here

因爲,負值你可以做 -

polar_data.pivot('topic', 'type')['negative'].plot(kind='bar', stacked=True) 

enter image description here