Pandas 堆叠条形图和分组条形图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29770073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas stacked barplot with grouped bars
提问by AAzza
I have the data that looks like this:
我有看起来像这样的数据:
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
I need to draw a bar plot that compares positive/negative values for different typefor each topic. I see it like stacked (positive/negative) barplot with topicon x axis and bars are grouped using typecolumn. But I could not find a way to build both grouped and stacked bar plot.
我需要绘制一个条形图,比较type每个topic. 我认为它像topicx 轴上的堆叠(正/负)条形图,条形图使用type列进行分组。但是我找不到同时构建分组条形图和堆叠条形图的方法。
For single type in looks like this (sorry I don't have enough reputation to post images):
对于单一类型的输入看起来像这样(对不起,我没有足够的声誉来发布图片):
polar_data.set_index(['type', 'topic']).xs('summary').plot(kind='bar', stacked=True)
And the only way I currently could compare two different types is only by placing two plots side by side using seaborn.factorplot, which doesn't allow to clearly notice the trends. And also I don't know how to build stacked bar plot with seaborn.
我目前可以比较两种不同类型的唯一方法是使用 并排放置两个图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')
So it there a way to "merge" them instead of place side by side?
那么有没有办法“合并”它们而不是并排放置?
回答by Zero
Here's one way to do it using matplotlib. I guess seaborn would use the same structure.
这是使用 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
So, now for positive values you could do -
所以,现在对于正值,你可以做 -
polar_data.pivot('topic', 'type')['positive'].plot(kind='bar', stacked=True)


For, negative values you could do -
对于,你可以做的负值 -
polar_data.pivot('topic', 'type')['negative'].plot(kind='bar', stacked=True)



