
Stacked bar chart similar to Excel
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Devanuj Deka
2021 年 7 月 16 日
@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 件のコメント
Devanuj Deka
2021 年 7 月 16 日
編集済み: Devanuj Deka
2021 年 7 月 16 日
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.

その他の回答 (1 件)
Devanuj Deka
2021 年 7 月 15 日
編集済み: Devanuj Deka
2021 年 7 月 15 日
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 件のコメント
Devanuj Deka
2021 年 7 月 16 日
編集済み: Devanuj Deka
2021 年 7 月 16 日
@AHMED FAKHRI, I have posted another answer with a code. Please check that out and let me know if you can work with this.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!