简体   繁体   English

如何使用 seaborn 为分类数据绘制堆叠的 100% 条形图

[英]How to plot stacked 100% bar plot with seaborn for categorical data

I have a dataset that looks like this (assume this has 4 categories in Clicked , the head(10) only showed 2 categories):我有一个看起来像这样的数据集(假设它在Clicked有 4 个类别, head(10)只显示 2 个类别):

    Rank Clicked
0   2.0 Cat4
1   2.0 Cat4
2   2.0 Cat4
3   1.0 Cat1
4   1.0 Cat4
5   2.0 Cat4
6   2.0 Cat4
7   3.0 Cat4
8   5.0 Cat4
9   5.0 Cat4

This is a code that returns this plot:这是返回此图的代码:

eee = (df.groupby(['Rank','Clicked'])['Clicked'].count()/df.groupby(['Rank'])['Clicked'].count())
eee.unstack().plot.bar(stacked=True)
plt.legend(['Cat1','Cat2','Cat3','Cat4'])
plt.xlabel('Rank')

在此处输入图片说明

Is there a way to achieve this with seaborn (or matplotlib) instead of the pandas plotting capabilities?有没有办法用seaborn(或matplotlib)而不是pandas绘图功能来实现这一点? I tried a few ways, both of running the seaborn code and of preprocessing the dataset so it's on the correct format, with no luck.我尝试了几种方法,包括运行 seaborn 代码和预处理数据集,使其格式正确,但没有运气。

Seaborn doesn't support stacked barplot, so you need to plot the cumsum: Seaborn 不支持堆叠条形图,因此您需要绘制 cumsum:

# calculate the distribution of `Clicked` per `Rank`
distribution = pd.crosstab(df.Rank, df.Clicked, normalize='index')

# plot the cumsum, with reverse hue order
sns.barplot(data=distribution.cumsum(axis=1).stack().reset_index(name='Dist'),
            x='Rank', y='Dist', hue='Clicked',
            hue_order = distribution.columns[::-1],   # reverse hue order so that the taller bars got plotted first
            dodge=False)

Output:输出:

在此处输入图片说明

Preferably, you can also reverse the cumsum direction, then you don't need to reverse hue order:最好,您还可以反转 cumsum 方向,那么您就不需要反转色调顺序:

sns.barplot(data=distribution.iloc[:,::-1].cumsum(axis=1)       # we reverse cumsum direction here
                       .stack().reset_index(name='Dist'),
            x='Rank', y='Dist', hue='Clicked',
            hue_order=distribution.columns,                     # forward order
            dodge=False)

Output:输出:

在此处输入图片说明

eg例如

tips = sns.load_dataset("tips")
sns.histplot(
    data=tips,
    x="size", hue="day",
    multiple="fill", stat="proportion",
    discrete=True, shrink=.8
)

在此处输入图片说明

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

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