Use both stacked and grouped Bars in one Plot

Hi,
I would like to plot my data like this:
I want to stack the two blue Bars and group blue, grey and green.
The data I want to use is in the attached file in
out.emissions.today{:,[7, 10,12,13]} of which 10 and 12 should be stacked.
I found this function written by a user, but I don't understand how I could insert my data into the function.

 Accepted Answer

Maybe something along these lines. Adjust as necessary.
load out
figure('Position',[1 1 1000 400])
N = size(out.emissions.today,1);
h_s = bar(out.emissions.today{:,[10 12]},'stacked','BarWidth',1/3,'XData',(1:N)-1/3,'FaceAlpha',0.5,'EdgeColor','none');
h_s(1).FaceColor = [0 0 0.5];
h_s(2).FaceColor = [0 0 1];
hold on
h_us = bar(out.emissions.today{:,[7 13]},'BarWidth',1,'XData',(1:N)+1/6,'FaceAlpha',0.5,'EdgeColor','none');
h_us(1).FaceColor = [0 0.5 0];
h_us(2).FaceColor = [0.5 0.5 0.5];
legend({ ...
'THG-Emissionen der Stromerzeugung (351 g/kWh)', ...
'THG-Emissionen durch Leckage des Kaltemittels', ...
'THG-Emissionen der Biomassebereitstellung (12 g/kWh)', ...
'Gesamt-THG-Emissionen'}, ...
'Location','SouthOutside','NumColumns',2,'Orientation','Horizontal')

4 Comments

Awesome, thanks a lot!!
Could you give a short explenation to what you are doing with 'XData' in these two lines (so I can learn and not just copy ;))?
h_s = bar(out.emissions.today{:,[10 12]},'stacked','BarWidth',1/3,'XData',(1:N)-1/3,'FaceAlpha',0.5,'EdgeColor','none');
h_us = bar(out.emissions.today{:,[7 13]},'BarWidth',1,'XData',(1:N)+1/6,'FaceAlpha',0.5,'EdgeColor','none');
The default XData of a bar plot is 1:N, that is, 1 through the number of bars (in this case 1:29). I shift the XData of the stacked bars to the left by 1/3 and shift the XData of the grouped bars to the right by 1/6 in order to get them to line up properly with each other and with the XTicks of the axes.
Alright, I understand. Thanks!
You're welcome!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 14 Aug 2023

Commented:

on 15 Aug 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!