Stacked bar chart similar to Excel

4 views (last 30 days)
Hi
I have the attached Excel sheet contains data to plot a stacked bar chart.
I want to plot it like the below picture from Excel:
Can I do that in MATLAB please?
Thanks

Accepted Answer

Devanuj Deka
Devanuj Deka on 16 Jul 2021
@AHMED FAKHRI, You can give this a try. The resultant output figure is shown below the code.
T = readtable('Data.xlsx');
t2 = rows2vars(T);
x1 = string(zeros(1,9));
x2 = x1;
leg = string(zeros(1,8));
for i=1:9
name = t2{i+1,1};
x1(i) = name{1};
name = t2{i+10,1};
x2(i) = name{1};
if i<9
name = t2{1,i+1};
leg(i) = name{1};
end
end
mat_1 = cell2mat(t2{2:10,2:9});
mat_2 = cell2mat(t2{11:19,2:9});
a1 = categorical(x1);
a2 = categorical(x2);
subplot(1,2,1)
bar(a1,mat_1,'stacked');
title('2035')
legend(leg,'Location','southoutside','Orientation','horizontal');
legend('boxoff');
subplot(1,2,2)
bar(a2,mat_2,'stacked');
title('2050')
legend(leg,'Location','southoutside','Orientation','horizontal');
legend('boxoff');
It gives a figure with two stacked bar charts, one for the year 2035, and the other for 2050, each with its legend below it.
  3 Comments
AHMED FAKHRI
AHMED FAKHRI on 16 Jul 2021
@Devanuj Deka Many thanks, yes I have been able to change the colour and the legends,, still one small issue please, how to remove the subscript 1-9 in the 2050 chart? I think that is because the MATLAB table does not like columns with the same name.
Devanuj Deka
Devanuj Deka on 16 Jul 2021
Edited: Devanuj Deka on 16 Jul 2021
It's simple. I chose the x-coordinate markers from the column names in your Data.xlsx file. In the plot, what appears as the markers depends on the first argument in bar(x,y,style). In the code that I shared:
subplot(1,2,1)
b1 = bar(a1,mat_1,'stacked'); % 'a1' is a categorical array of the names of the FIRST 9 COLUMNS
b1(8).FaceColor = [0 0 0];
title('2035')
legend(leg,'Location','southoutside','Orientation','horizontal');
legend('boxoff');
subplot(1,2,2)
bar(a2,mat_2,'stacked'); % 'a2' is a categorical array of the names of the LAST 9 COLUMNS
title('2050')
legend(leg,'Location','southoutside','Orientation','horizontal');
legend('boxoff');
you might notice that in the first subplot I've given the names of the first 9 columns (Sce1, Sce2, etc.) from Data as the x vector, and i the second subplot I've given the names of the last 9 columns (Sce1_1, Sce2_1, etc.).
Simply replace 'a2' in the second bar function call with 'a1', so that the 2050 plot also has Sce1, Sce2 etc. as the x-coordinate markers. The modified code will look like:
subplot(1,2,1)
b1 = bar(a1,mat_1,'stacked'); % 'a1' is a categorical array of the names of the FIRST 9 COLUMNS
b1(8).FaceColor = [0 0 0];
title('2035')
legend(leg,'Location','southoutside','Orientation','horizontal');
legend('boxoff');
subplot(1,2,2)
bar(a1,mat_2,'stacked'); % 'a2' is a categorical array of the names of the LAST 9 COLUMNS
title('2050')
legend(leg,'Location','southoutside','Orientation','horizontal');
legend('boxoff');
Resultant figure: you'll see that there are no subscripts now.

Sign in to comment.

More Answers (1)

Devanuj Deka
Devanuj Deka on 15 Jul 2021
Edited: Devanuj Deka on 15 Jul 2021
Yes, you can. MATLAB's bar function has an optional style parameter that you can use to display stacked bar chart. Here is the documentation: bar(___,style)
If you specify the style parameter as 'stacked', you can create a stacked bar chart. For more information: style
  2 Comments
AHMED FAKHRI
AHMED FAKHRI on 15 Jul 2021
Thanks , i tried that before posting the post and it requires much more than that
Devanuj Deka
Devanuj Deka on 16 Jul 2021
Edited: Devanuj Deka on 16 Jul 2021
@AHMED FAKHRI, I have posted another answer with a code. Please check that out and let me know if you can work with this.

Sign in to comment.

Categories

Find more on Line 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!