如何从 df.groupby('feature')['label'].value_counts() 绘制堆积条形图

Bow*_*eng 3 python matplotlib pandas seaborn

我得到df.groupby('feature')['label'].value_counts()如下:

X_train.groupby('reqrealip_by_pkgname_count')['label'].value_counts()

22052                       1.0      15390
                            0.0       4512
22179                       1.0      15419
                            0.0       4644
22215                       1.0      15514
                            0.0       4619
22231                       1.0      15505
                            0.0       4604
22249                       1.0      15562
                            0.0       4678
22304                       1.0      15632
                            0.0       4642
22331                       1.0      15551
                            0.0       4680
22364                       1.0      15618
                            0.0       4689
22367                       1.0      15537
                            0.0       4715
22394                       1.0      15650
                            0.0       4627
22584                       0.0      12630
                            1.0       7644
26040                       1.0      21339
                            0.0       2266
32176                       1.0      27082
                            0.0       2014
Run Code Online (Sandbox Code Playgroud)

我想像这样画一个堆积条形图。
但该图中每个条形的高度并不相等。
如果不介意,有人可以帮助我如何绘制这样的堆叠条形图吗?
提前致谢。

堆积条形图

jez*_*ael 7

Series.unstack与以下一起使用DataFrame.plot.bar

s = X_train.groupby('reqrealip_by_pkgname_count')['label'].value_counts()
Run Code Online (Sandbox Code Playgroud)

如果需要标准化,请将参数添加normalizeSeries.value_counts并乘以100

s=X_train.groupby('reqrealip_by_pkgname_count')['label'].value_counts(normalize=True).mul(100)
Run Code Online (Sandbox Code Playgroud)
s.unstack().plot.bar(stacked=True)
Run Code Online (Sandbox Code Playgroud)

或者:

s.unstack(0).plot.bar(stacked=True)
Run Code Online (Sandbox Code Playgroud)