Plot stacked bar chart with legends

41 views (last 30 days)
Hi All
I have the atatched Excel sheet and I want please to plot a stacked bar chart in every year (2025, 2030, 2040, 2050) according to the three techs (electrics, H2, CCS+BECCS). I want the legends to be unique in terms of the colour and do not change if the year or the tech is changed which is where I mostly struggeling.
I don't mind using other plotting method if easier.
I tried using the following code but the legends will be different for each sector.
bar(1,[E25{:,2}],'stacked')
tech=['Electric'];
set(gca,'xticklabel',tech)
legend (E25{:,1})
hold on
bar(2,[H25{:,2}],'stacked')
tech2=['Hydrogen'];
set(gca,'xticklabel',tech2)
hold on
bar(3,[CCS25{:,2}],'stacked')
tech3=['CCS+BECCS'];
set(gca,'xticklabel',tech3)
The final graph should looks like this:

Accepted Answer

Adam Danz
Adam Danz on 22 May 2021
Edited: Adam Danz on 22 May 2021
Legend demo
Create the bar plots and assign the bar colors. Combine the bar handles within the same legend and specify the legend strings. This is done with tiledlayout where you can more easily control the position of a global legend.
rng('default')
x1 = rand(3,8);
x2 = rand(4,10);
fig = figure();
fig.Position(3:4) = [725,420];
tiledlayout(1,2)
nexttile
bh1 = bar(x1,'stacked');
colors1 = mat2cell(lines(numel(bh1)),ones(numel(bh1),1), 3);
set(bh1, {'CData'}, colors1)
nexttile
bh2 = bar(x2,'stacked');
set(bh2,'FaceColor','flat');
colors2 = mat2cell(colorcube(numel(bh2)),ones(numel(bh2),1), 3);
set(bh2, {'CData'}, colors2)
% Define one label per bar handle
legHandles = [bh1, bh2];
labels = ["Cement","Ethylene","Glass","Lime","Gas","Oil","Paper","Vehicles","Coal",...
"Ammonia","Shale Gas","NRMM","Foo","Bar","Other","Gluposti","Waste","Refining"];
lg = legend(legHandles,labels,'Orientation','Horizontal','NumColumns',6);
lg.Layout.Tile = 'North';
Colorbar demo
rng('default')
x1 = rand(3,8);
x2 = rand(4,10);
fig = figure();
fig.Position(3:4) = [725,420];
tiledlayout(1,2)
nexttile
bh1 = bar(x1,'stacked');
colors1 = mat2cell(lines(numel(bh1)),ones(numel(bh1),1), 3);
set(bh1, {'CData'}, colors1)
nexttile
bh2 = bar(x2,'stacked');
set(bh2,'FaceColor','flat');
colors2 = mat2cell(colorcube(numel(bh2)),ones(numel(bh2),1), 3);
set(bh2, {'CData'}, colors2)
% Define one label per color
allColors = [vertcat(colors1{:}); vertcat(colors2{:})];
labels = ["Cement","Ethylene","Glass","Lime","Gas","Oil","Paper","Vehicles","Coal",...
"Ammonia","Shale Gas","NRMM","Foo","Bar","Other","Gluposti","Waste","Refining"];
% Combine bar color and use them to define the colormap
ax = gca;
ax.Colormap = allColors;
cb = colorbar();
cmapInterval = 1/size(allColors,1);
cb.Ticks = cmapInterval/2 : cmapInterval : 1;
cb.TickLabels = labels;
  8 Comments
AHMED FAKHRI
AHMED FAKHRI on 22 May 2021
To be clear, the graphs I quoted are not plotted by me, these are plotted by the orignal produced and I am trying to replicate them. Off course there will be differences in terms of the values because how they interepret electric techs or H2 techs is different to what I interepret.
The code I included just as an example of my old why which did not work of course, So I shared the Excel data instead but it was just indicative.
Many thanks for the last code, it seems this is what I want, I will check now and accept your answer, I really thanks you and apologize for the misunderstanding
Adam Danz
Adam Danz on 23 May 2021
I see. I hope you make better color choices than they did 😄.
There are a bunch of colormap functions on the file exchange that may help.

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

R2020b

Community Treasure Hunt

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

Start Hunting!