for loop for generate overlay bar graphs

I generated a bar graph of the following type:
load matrix.mat
matrix = matrix(1:2,:);
x = matrix(:,1)';
wR = 0.5;
wB = 0.6;
wG = 0.7;
wT = 0.8;
temp_R = matrix(:,2);
temp_B = matrix(:,3);
temp_G = matrix(:,4);
temp_T = matrix(:,5);
figure
barh(x,temp_T,wT,'FaceColor',[1,0,0.52])
hold on
barh(x,temp_G,wG,'FaceColor',[0,1,0])
barh(x,temp_B,wB,'FaceColor',[0,0,1])
barh(x,temp_R,wR,'FaceColor',[1,0,0])
hold off
I need to transform it so that the graph becomes this:
I tried the following code but it only works correctly for the last row of 'matrix' (i.e., for the value 67 on the vertical axis).
figure
barh(x,temp_T,wT,'FaceColor',[1,0,0.52])
for K = 1:height(matrix)
matrix_only_row = matrix(K,:);
if matrix_only_row(1,2) > ( matrix_only_row(1,3) && matrix_only_row(1,4) )
if matrix_only_row(1,3) > matrix_only_row(1,4)
hold on
wR = 0.7;
barh(x,temp_R,wR,'FaceColor',[1,0,0])
wB = 0.6;
barh(x,temp_B,wB,'FaceColor',[0,0,1])
wG = 0.5;
barh(x,temp_G,wG,'FaceColor',[0,1,0])
hold off
elseif matrix_only_row(1,4) > matrix_only_row(1,3)
hold on
wR = 0.7;
barh(x,temp_R,wR,'FaceColor',[1,0,0])
wG = 0.6;
barh(x,temp_G,wG,'FaceColor',[0,1,0])
wB = 0.5;
barh(x,temp_B,wB,'FaceColor',[0,0,1])
hold off
end
end
end
What do I need to modify to get the desired result?
If there is a more direct way it is appreciated.

 Réponse acceptée

Here's one way, using patches instead of barh:
load matrix.mat
[data,idx] = sort(matrix(:,2:end),2,'descend');
[n,m] = size(data);
colors = [1 0 0; 0 0 1; 0 1 0; 1 0 0.52];
max_bar_width = 0.8;
min_bar_width = 0.5;
bar_widths = linspace(max_bar_width,min_bar_width,m);
for ii = 1:n
for jj = 1:m
patch([0 0 data(ii,jj)*[1 1] 0],matrix(ii,1)+bar_widths(jj)*0.5*[-1 1 1 -1 -1],colors(idx(ii,jj),:));
end
end

6 commentaires

Alberto Acri
Alberto Acri le 22 Août 2023
Hi @Voss. Is it possible to change the width of the bars? In the code it goes from 0.8 down to 0.5 (so 0.8 - 0.7 - 0.6 - 0.5). I would like to be able to change the width of the bars so that we have 0.9 - 0.7 - 0.5 - 0.3 (step of 2 and not 1).
Voss
Voss le 22 Août 2023

Yes, simply use:

max_bar_width = 0.9;
min_bar_width = 0.3;
bar_widths = linspace(max_bar_width,min_bar_width,m);
Okay, thank you. And one more question.
I put the legend to the chart but the colors in the legend do not match the colors entered in 'colors':
legend('SECTION 1', 'SECTION 2', 'SECTION 3', 'SECTION 4', 'Location','southeast')
The colors are correct when the bar graph is created, but they are not correct in the legend.
They must be:
  • red for SECTION 1
  • blue for SECTION 2
  • green for SECTION 3
  • magenta for SECTION 4
Voss
Voss le 22 Août 2023
Modifié(e) : Voss le 22 Août 2023
load matrix.mat
[data,idx] = sort(matrix(:,2:end),2,'descend');
[n,m] = size(data);
colors = [1 0 0; 0 0 1; 0 1 0; 1 0 0.52];
max_bar_width = 0.9;
min_bar_width = 0.3;
bar_widths = linspace(max_bar_width,min_bar_width,m);
p = zeros(n,m);
for ii = 1:n
for jj = 1:m
p(ii,idx(ii,jj)) = patch([0 0 data(ii,jj)*[1 1] 0],matrix(ii,1)+bar_widths(jj)*0.5*[-1 1 1 -1 -1],colors(idx(ii,jj),:));
end
end
legend(p(1,:),'SECTION 1', 'SECTION 2', 'SECTION 3', 'SECTION 4', 'Location','southeast')
Alberto Acri
Alberto Acri le 22 Août 2023
Perfect! Is there also a way to do the same thing using barh?
Voss
Voss le 22 Août 2023
Modifié(e) : Voss le 22 Août 2023
load matrix.mat
[data,idx] = sort(matrix(:,2:end),2,'descend');
[n,m] = size(data);
colors = [1 0 0; 0 0 1; 0 1 0; 1 0 0.52];
max_bar_width = 0.9;
min_bar_width = 0.3;
bar_widths = linspace(max_bar_width,min_bar_width,m);
hold on
b = zeros(n,m);
for ii = 1:n
for jj = 1:m
b(ii,idx(ii,jj)) = barh(matrix(ii,1),data(ii,jj),bar_widths(jj),'FaceColor',colors(idx(ii,jj),:));
end
end
legend(b(1,:),'SECTION 1', 'SECTION 2', 'SECTION 3', 'SECTION 4', 'Location','southeast')

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Centre d'aide et File Exchange

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by