Pat Pat - 1 year ago 209
Python Question

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

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

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

s_sort = s.groupby(level=[0]).sum().sort_values(ascending=False)
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 ='jet')
s.reindex(index=s_sort.index, level=0).unstack(), cmap=cmp)

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download