简体   繁体   English

使用Pandas交叉表与seaborn堆积的条形图

[英]Using Pandas crosstab with seaborn stacked barplots

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. 我想使用seaborn来创建一个堆积的条形图以保持一致,这就是我用于其余图形的内容。 I have struggled to do this however as I am unable to index the crosstab. 我一直在努力做到这一点,因为我无法索引交叉表。

I have been able to make the plot I want in pandas using .plot.barh(stacked=True) but no luck with seaborn. 我已经能够使用.plot.barh(stacked=True)在熊猫中制作我想要的情节但是没有运气与seaborn。 Any ideas how i can do this? 我有什么想法可以做到这一点?

Thanks 谢谢

As you said you can use pandas to create the stacked bar plot. 正如您所说,您可以使用pandas来创建堆积条形图。 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图和每个pandas图最终只是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()

在此输入图像描述

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)

分组条形图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM