Azzera filtri
Azzera filtri

Stacked bar chart similar to Excel

6 visualizzazioni (ultimi 30 giorni)
AHMED FAKHRI
AHMED FAKHRI il 15 Lug 2021
Modificato: Devanuj Deka il 16 Lug 2021
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

Risposta accettata

Devanuj Deka
Devanuj Deka il 16 Lug 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 Commenti
AHMED FAKHRI
AHMED FAKHRI il 16 Lug 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 il 16 Lug 2021
Modificato: Devanuj Deka il 16 Lug 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.

Accedi per commentare.

Più risposte (1)

Devanuj Deka
Devanuj Deka il 15 Lug 2021
Modificato: Devanuj Deka il 15 Lug 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 Commenti
AHMED FAKHRI
AHMED FAKHRI il 15 Lug 2021
Thanks , i tried that before posting the post and it requires much more than that
Devanuj Deka
Devanuj Deka il 16 Lug 2021
Modificato: Devanuj Deka il 16 Lug 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.

Accedi per commentare.

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by