I have a pandas dataframe, with MultiIndex:
import numpy as np
import pandas as pd
multi_idx = pd.MultiIndex.from_arrays([['A', 'A', 'B', 'B', 'B', 'C'],
['a1', 'a2', 'b1', 'b2', 'b3', 'c']])
index = ['one', 'two', 'three', 'four']
data = pd.DataFrame(np.random.randint(0, 10, (len(index), len(multi_idx))),
index=index, columns=multi_idx)
A B C
a1 a2 b1 b2 b3 c
one 6 8 6 4 9 5
two 6 9 7 7 4 9
three 3 2 5 8 2 6
four 1 7 0 9 2 7
I want to represent the data as a bar chart that with stacked bars within each label of first level of columns [A, B, C].
So my desired output is a single chart with:
- 4 groups of bars (one per index)
- 3 bars within each group [A, B, C]
- each bar being the stacked values for subgroups (e.g. [a1, a2] for A)
This is a schematic representation of my desired output, just to clarify how I want to group/stack the variables (labels are for illustration only...)
Any help would be greatly appreciated.