Plot stacked bar chart with legends
35 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
AHMED FAKHRI
el 22 de Mayo de 2021
Comentada: Adam Danz
el 23 de Mayo de 2021
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 comentarios
Respuesta aceptada
Adam Danz
el 22 de Mayo de 2021
Editada: Adam Danz
el 22 de Mayo de 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 comentarios
Adam Danz
el 23 de Mayo de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!