带有排序值的 Pandas 堆积条形图

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

Pandas stacked bar chart with sorted values

pythonpandasdataframebar-chartstacked

提问by Pat

My goal is to create a stacked bar chart of a multilevel dataframe. The dataframe looks like this:

我的目标是创建一个多级数据框的堆积条形图。数据框如下所示:

import pandas as pd
import numpy as np

arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux', 'qux']),
          np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two', 'three'])]

s = pd.Series([10,20,10,22,10,24,10,26, 11], index=arrays)

In[1]: s

Out[1]: 
bar  one      10
     two      20
baz  one      10
     two      22
foo  one      10
     two      24
qux  one      10
     two      26
     three    11
dtype: int64

I have two goals:

我有两个目标:

  1. create a stacked bar chart such that the values are stacked to 4 single bins called bar, baz, foo, qux.

  2. the 4 bars should be ordered by size. In this example, the quxbar will have height (10+26+11=)47 and should be the first left, followed by the foobar which has height (10+24)=34.

  1. 创建一个堆叠条形图,以便将值堆叠到 4 个称为barbazfoo、 的单个箱中qux

  2. 4 根钢筋应按尺寸订购。在此示例中,qux条形的高度为 (10+26+11=)47 并且应该是第一个左侧,然后是foo高度为 (10+24)=34的条形。

回答by Nickil Maveli

  1. Sorting the first level index according to it's total sum:
  1. 根据总和对一级索引进行排序:


s_sort = s.groupby(level=[0]).sum().sort_values(ascending=False)
s_sort
qux    47
foo    34
baz    32
bar    30
dtype: int64
  1. Reindex back using the new sorted index values in the first level + unstack+ plot:
  1. 使用第一级unstack++ 图中的新排序索引值重新索引:


cmp = plt.cm.get_cmap('jet')
s.reindex(index=s_sort.index, level=0).unstack().plot.bar(stacked=True, cmap=cmp)

enter image description here

在此处输入图片说明

回答by Ashish Gulati

One small addition to the game: we could sort at the inner index level by the values too

游戏的一个小补充:我们也可以按值在内部索引级别进行排序

s1=s.groupby(level=[0]).apply(lambda x:x.groupby(level=[1]).sum().sort_values(ascending=False))
s1

The inner level now stands sorted.

内层现在已排序。

bar  two      20
     one      10
baz  two      22
     one      10
foo  two      24
     one      10
qux  two      26
     three    11
     one      10
dtype: int64

Now we sort by the outer level in the already mentioned way.

现在我们按照已经提到的方式按外层排序。

s_sort = s1.groupby(level=[0]).sum().sort_values(ascending=False)
s2 = s1.reindex(index=s_sort.index, level=0)
s2

qux  two      26
     three    11
     one      10
foo  two      24
     one      10
baz  two      22
     one      10
bar  two      20
     one      10
dtype: int64

Unfortunately, matplotlib plays the spoil-sport by messing up the order of the stacked bars on its own X(

不幸的是,matplotlib 通过在自己的 X(

s2.unstack().plot.bar(stacked=True)

Stacked Bar Chart

堆积条形图