简体   繁体   中英

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:

在此处输入图像描述

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.

Thanks in advance!

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM