transform bar chart with separate bars (for each value of y) to bar chart with overlapping bars

5 visualizaciones (últimos 30 días)
Hi. I need to transform this figure:
load matrix.mat
matrix = matrix(5:7,:);
figure();
cm = [1,0,0; 0,0,1; 0,1,0; 1,0,0.52];
hbh = barh(matrix(:,1), matrix(:,[2 3 4 5]));
for k = 1:numel(hbh)
hbh(k).FaceColor = cm(k,:);
hbh(k).EdgeColor = [0,0,0];
end
into this one:
I tried the following way but I don't get the same result.
figure
hbh = barh(matrix(:,1), matrix(:,[2 3 4 5]),'stacked');
cm = [[1,0,0]; [0,0,1]; [0,1,0]; [1,0,0.52]];
for k = 1:numel(hbh)
hbh(k).FaceColor = cm(k,:);
hbh(k).EdgeColor = [0,0,0];
end
  6 comentarios
the cyclist
the cyclist el 21 de Ag. de 2023
I understand. But after the sort, a negative diff can't occur. (But if I am wrong about the sorting, then the max would be required, as you state.)

Iniciar sesión para comentar.

Respuesta aceptada

the cyclist
the cyclist el 20 de Ag. de 2023
load matrix
M = matrix(:,2:end);
[sortedM,sortingIndex] = sort(M,2);
cm = [[1,0,0]; [0,0,1]; [0,1,0]; [1,0,0.52]];
figure
hold on
for mi = 1:height(sortedM)
hbh = barh(matrix(mi,1), [sortedM(mi,1),diff(sortedM(mi,:),1,2)],'stacked');
for k = 1:width(sortedM)
hbh(k).FaceColor = cm(sortingIndex(mi,k),:);
end
end
  2 comentarios
Alberto Acri
Alberto Acri el 21 de Ag. de 2023
Thank you @the cyclist. That is the result I wanted.
Right now I prefer to use the graph with the bars overlaid with the code you provided because I have the desired values on the x-axis. Of course, it is not easy to read, in fact I will modify it with the documentation you attached.
I asked the question here because I have a problem with the visualization. If you want you can take a look at it.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by