Stacked bar chart similar to Excel

조회 수: 5 (최근 30일)
AHMED FAKHRI
AHMED FAKHRI 2021년 7월 15일
편집: Devanuj Deka 2021년 7월 16일
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

채택된 답변

Devanuj Deka
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
AHMED FAKHRI
AHMED FAKHRI 2021년 7월 16일
@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 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
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
AHMED FAKHRI
AHMED FAKHRI 2021년 7월 15일
Thanks , i tried that before posting the post and it requires much more than that
Devanuj Deka
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by