计数带色调的堆积条形图 [英] count plot with stacked bars per hue
问题描述
我正在寻找一种根据色相"绘制带有堆叠条形图的计数图的有效方法. 标准的色相行为是根据第二列的值将计数分成多个平行条,我正在寻找一种有效的方式来堆叠色相条以便快速比较总数.
I am looking for an efficient way of drawing a count plot with stacked bars according to "hue". Standard hue behavior is to split a count into parallel bars according to the value of a second column, what I am looking for is an efficient way to have the hue bars stacked in order to quickly compare totals.
让我用泰坦尼克号数据集的示例进行解释:
Let me explain with an example from the titanic dataset:
import pandas as pd
import numpy as np
import seaborn as sns
%matplotlib inline
df = sns.load_dataset('titanic')
sns.countplot(x='survived',hue='class',data=df)
通过计数图和色相给出标准的Seaborn行为
gives standard Seaborn behavior with countplot and hue
我正在寻找的是每色调的堆积条
what I am looking for is something like stacked bars per hue
要获取最后一张图片,我使用了以下代码
to get the last image I used the following code
def aggregate(rows,columns,df):
column_keys = df[columns].unique()
row_keys = df[rows].unique()
agg = { key : [ len(df[(df[rows]==value) & (df[columns]==key)]) for value in row_keys]
for key in column_keys }
aggdf = pd.DataFrame(agg,index = row_keys)
aggdf.index.rename(rows,inplace=True)
return aggdf
aggregate('survived','class',df).plot(kind='bar',stacked=True)
我确信有一些更有效的方法. 我知道seaborn并不是非常友好的堆叠式吧台,所以我尝试用我的函数重新排列数据集并使用matplotlib,但是我想还有一种更聪明的方法.
I am sure there is some more efficient way. I know seaborn is not very stacked bars friendly... so I tried to rearrange the dataset with my function and used matplotlib, but I guess there is a more clever way to do that as well.
非常感谢!
推荐答案
您的最后一部分基本上就在那儿,将DataFrame.plot()
与bar
和stacked=True
一起使用.
You were basically there with your last part, using DataFrame.plot()
with bar
and stacked=True
.
您可以用groupby
+ pivot
代替您的aggregate
函数.
Instead of your aggregate
function, you can accomplish what you want with a groupby
+ pivot
.
df_plot = df.groupby(['class', 'survived']).size().reset_index().pivot(columns='class', index='survived', values=0)
class First Second Third
survived
0 80 97 372
1 136 87 119
在这里,您可以使用stacked=True
参数将其绘制为bar
From here you can just plot it as a bar
with the stacked=True
argument
df_plot.plot(kind='bar', stacked=True)
这篇关于计数带色调的堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!