Search code examples
pythonpandasdataframebar-chartstacked

Pandas stacked bar chart with sorted values


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 qux bar will have height (10+26+11=)47 and should be the first left, followed by the foo bar which has height (10+24)=34.


Solution

    1. Sorting the first level index according to it's total sum:

    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:

    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