Python 将 Pandas 交叉表与 seaborn 堆叠条形图结合使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43544694/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:10:51  来源:igfitidea点击:

Using Pandas crosstab with seaborn stacked barplots

pythonpandasmatplotlibseaborn

提问by JB1

I am trying to create a stacked barplot in seaborn with my dataframe.

我正在尝试使用我的数据框在 seaborn 中创建一个堆叠的条形图。

I have first generated a crosstab table in pandas like so:

我首先在 Pandas 中生成了一个交叉表,如下所示:

pd.crosstab(df['Period'], df['Mark'])

which returns:

返回:

  Mark            False  True  
Period BASELINE    583    132
       WEEK 12     721      0 
       WEEK 24     589    132 
       WEEK 4      721      0

I would like to use seaborn to create a stacked barplot for congruence, ans this is what I have used for the rest of my graphs. I have struggled to do this however as I am unable to index the crosstab.

我想使用 seaborn 创建一个堆叠条形图以获得一致性,这是我用于其余图表的内容。然而,我一直在努力做到这一点,因为我无法索引交叉表。

I have been able to make the plot I want in pandas using .plot.barh(stacked=True)but no luck with seaborn. Any ideas how i can do this?

我已经能够在.plot.barh(stacked=True)Pandas 中制作我想要的情节,但在 seaborn 上没有运气。任何想法我怎么能做到这一点?

Thanks

谢谢

回答by ImportanceOfBeingErnest

As you said you can use pandas to create the stacked bar plot. The argument that you want to have a "seaborn plot" is irrelevant, since every seaborn plot and every pandas plot are in the end simply matplotlib objects, as the plotting tools of both libraries are merely matplotlib wrappers.

正如您所说,您可以使用熊猫来创建堆积条形图。你想要一个“seaborn plot”的论点是无关紧要的,因为每个 seaborn plot 和每个 pandas plot 最终都只是 matplotlib 对象,因为两个库的绘图工具只是 matplotlib 包装器。

So here is a complete solution (taking the datacreation from @andrew_reece's answer).

所以这是一个完整的解决方案(从@andrew_reece 的答案中获取数据)。

import numpy as np 
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

n = 500
mark = np.random.choice([True,False], n)
periods = np.random.choice(['BASELINE','WEEK 12', 'WEEK 24', 'WEEK 4'], n)

df = pd.DataFrame({'mark':mark,'period':periods})
ct = pd.crosstab(df.period, df.mark)

ct.plot.bar(stacked=True)
plt.legend(title='mark')

plt.show()

enter image description here

在此处输入图片说明

回答by andrew_reece

The guy who created Seaborn doesn't like stacked bar charts(but that link has a hack which uses Seaborn + Matplotlib to make them anyway).

创建 Seaborn 的人不喜欢堆叠条形图(但该链接有一个 hack,它使用 Seaborn + Matplotlib 来制作它们)。

If you're willing to accept a grouped bar chart instead of a stacked one, here's one approach:

如果您愿意接受分组条形图而不是堆叠条形图,这里有一种方法:

 # first some sample data
 import numpy as np 
 import pandas as pd
 import seaborn as sns

 N = 1000
 mark = np.random.choice([True,False], N)
 periods = np.random.choice(['BASELINE','WEEK 12', 'WEEK 24', 'WEEK 4'], N)

 df = pd.DataFrame({'mark':mark,'period':periods})
 ct = pd.crosstab(df.period, df.mark)

 mark      False  True 
 period                
 BASELINE    118    111
 WEEK 12     117    149
 WEEK 24     117    130
 WEEK 4      127    131

 # now stack and reset
 stacked = ct.stack().reset_index().rename(columns={0:'value'})

 # plot grouped bar chart
 sns.barplot(x=stacked.period, y=stacked.value, hue=stacked.mark)

grouped bar chart

分组条形图