Plot stacked bar chart with legends
조회 수: 6 (최근 30일)
이전 댓글 표시
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:

댓글 수: 0
채택된 답변
Adam Danz
2021년 5월 22일
편집: Adam Danz
2021년 5월 22일
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
Adam Danz
2021년 5월 23일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!









