Search code examples
pythonpandasmatplotlibstacked-bar-chartgrouped-bar-chart

Combining two stacked bar plots for a grouped stacked bar plot


So I found the following code online:

import matplotlib.pyplot as plt
import matplotlib

matplotlib.style.use('ggplot')

plotdata = pd.DataFrame({
    "2018_m":[40, 12, 10, 26, 36],
    "2019_m":[19, 8, 30, 21, 38],
    "2020_m":[10, 10, 42, 17, 37]
    }, index=["Dad", "Mam", "Bro", "Sis", "Me"]
)

plotdata2 = pd.DataFrame({
    "2018_y":[20, 22, 10, 34, 12],
    "2019_y":[12, 19, 27, 35, 14],
    "2020_y":[21, 31, 52, 20, 34]
    }, index=["Dad", "Mam", "Bro", "Sis", "Me"]
)

stacked_data = plotdata.apply(lambda x: x*100/sum(x), axis=1)
stacked_data2 = plotdata2.apply(lambda x: x*100/sum(x), axis=1)
stacked_data.plot(kind="bar", stacked=True)
stacked_data2.plot(kind="bar", stacked=True)

And this is the output:

enter image description here

I was wondering what would be the best way to combine them so that Dad, Mam, Bro etc. each have two stacked bars? I've come across a bunch of other grouped stacked bar codes online and elsewhere on Stack Overflow but they require you to iteratively define which values you have for each bar, whereas ideally I'd want to just have to reference the dataframe names 'plotdata' and 'plotdata2' like in the code above.


Solution

  • For two groups, you can pass position and adjust the width accordingly:

    fig, ax = plt.subplots()
    
    stacked_data.plot(kind="bar", stacked=True, width=0.4, 
                      ax=ax, position=0)
    stacked_data2.plot(kind="bar", stacked=True, width=0.4, 
                       ax=ax, position=1, hatch='//')
    
    ax.set_xlim(right=len(stacked_data)-0.5)
    

    Output:

    enter image description here