Legend is not showing all the colors - bar plot

21 visualizaciones (últimos 30 días)
Marianna Pizzo
Marianna Pizzo el 7 de Mayo de 2023
Comentada: Marianna Pizzo el 8 de Mayo de 2023
Hi everyone!
My MATLAB code creates the bar plot you see below, however the legend outputs only two colors when in reality the bars are of 3 colors, how can I also get the legend for the blue color corresponding to 'Photos'. Thanks in advance, bye!
singleObjsPerc= rand(4,12);
TecniquesName = {'A', 'B', 'C', 'D', 'E', 'F'};
objName = {'Bottle', 'Sanitizer', 'Coffee cup', 'Notebook'};
for i = 1:4
subplot(2,2,i)
b=bar([singleObjsPerc(i,1:2); singleObjsPerc(i,3:4); singleObjsPerc(i,5:6); singleObjsPerc(i,7:8); singleObjsPerc(i,9:10); singleObjsPerc(i,11:12)],'FaceColor','flat');
aux=b(1,1);
aux.CData(1,:) = [0.9290, 0.6940, 0.1250];
title(objName{i});
set(gca,'XTickLabel',TecniquesName);
end
legend('Kinect','Photos', 'Video', 'Location', 'northwest')
Warning: Ignoring extra legend entries.

Respuesta aceptada

the cyclist
the cyclist el 7 de Mayo de 2023
Editada: the cyclist el 7 de Mayo de 2023
In each subplot, you are plotting
[singleObjsPerc(i,1:2); singleObjsPerc(i,3:4); singleObjsPerc(i,5:6); singleObjsPerc(i,7:8); singleObjsPerc(i,9:10); singleObjsPerc(i,11:12)]
which is a 6x2 array. Therefore, you are plotting two sets of bars, not three.
Then, the lines
aux=b(1,1);
aux.CData(1,:) = [0.9290, 0.6940, 0.1250]
change the color of the first bar of the first group of bars (but this color change has no bearing on the fact that there are only two sets of objects).
Therefore, your legend has only two objects to assign to (which is why you get the warning about too many legend entries).
It's not clear to me what you actually intended (and also I assume this is just a toy example), so I can't really give you any more advice than that, without more detail.
  4 comentarios
the cyclist
the cyclist el 7 de Mayo de 2023
I feel like we may be stumbling into the XY problem here. We are trying to help you fix your solution, rather than solve your fundamental problem.
It seems to me that your fundamental problem is that you are plotting two bars per group, but trying to assign three legend labels to them. Why aren't you plotting three bars, if you have three labels?
Here is a much-simplified way to plot three bars per group, with three labels correctly assigned. (Of course, I created more random data.) It seems to do everything you need. Is there anything wrong with it?
singleObjsPerc= rand(4,18);
TecniquesName = {'A', 'B', 'C', 'D', 'E', 'F'};
objName = {'Bottle', 'Sanitizer', 'Coffee cup', 'Notebook'};
for i = 1:4
subplot(2,2,i)
b2 = bar([singleObjsPerc(i,1:3); singleObjsPerc(i,4:6); singleObjsPerc(i,7:9); singleObjsPerc(i,10:12); singleObjsPerc(i,13:15); singleObjsPerc(i,16:18)], 'grouped');
title(objName{i})
xticklabels(TecniquesName')
% Combinazione delle legende
legend({'Kinect', 'Photos', 'Video'})
end
Marianna Pizzo
Marianna Pizzo el 8 de Mayo de 2023
Thanks a lot for your precious help, I fixed it by doing as you said changing my singleObjsPerc matrix from 4x12 to 4x18 by putting nans where I had no data. Thanks a lot for the explanation.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by