Showing legend even if data is not there

23 Ansichten (letzte 30 Tage)
Mathan
Mathan am 4 Mai 2022
Bearbeitet: Mathan am 4 Mai 2022
Hello,
Could anyone point out what I am doing wrong in the below code (I am trying to show all the legend entries even if there are no matching elements while I do the plotting. I used the idea followed here - https://www.mathworks.com/matlabcentral/answers/265197-force-a-legend-entry-even-if-there-are-no-matching-elements-in-the-plot). However I am getting the legend entry 'Data4' displayed twice (attached is the output).
a=(1:1:9)';
a=reshape(a,3,[]);
b=sin(a);
c=cos(a);
scatter(a,b,'.','DisplayName', 'sin')
hold on
scatter(a,c,'.','DisplayName','cos')
data1='Data1';
plot(a(2),b(2),'o','Color','r', 'MarkerSize',5,'DisplayName',data1)
data2='Data2';
plot(a(4),b(4),'o','Color','r','MarkerSize',10,'DisplayName',data2)
data3='Data3';
plot(a(6),b(6),'s','Color','k', 'MarkerSize',5,'DisplayName',data3)
data4='Data4';
index = find(a>=3 & a<=7);
plot([a(index), nan(size(a(index)))], [b(index), nan(size(b(index)))],'s','Color','k','MarkerSize',10,'DisplayName',data4)
% plot([a(a_index),nan], [b(a_index),nan],'s','Color','k','MarkerSize',10,'DisplayName',data4)
hold off
legend()
If I try:
plot([a(a_index),nan], [b(a_index),nan],'s','Color','k','MarkerSize',10,'DisplayName',data4)
then it shows Dimensions of arrays being concatenated are not consistent.
Thanks.

Akzeptierte Antwort

Chunru
Chunru am 4 Mai 2022
a=(1:1:9)';
a=reshape(a,3,[]);
b=sin(a);
c=cos(a);
scatter(a,b,'.','DisplayName', 'sin')
hold on
scatter(a,c,'.','DisplayName','cos')
data1='Data1';
plot(a(2),b(2),'o','Color','r', 'MarkerSize',5,'DisplayName','data1')
data2='Data2';
plot(a(4),b(4),'o','Color','r','MarkerSize',10,'DisplayName','data2')
data3='Data3';
plot(a(6),b(6),'s','Color','k', 'MarkerSize',5,'DisplayName','data3')
%data4='Data4';
a_index = find(a>=3 & a<=7);
%plot([a(index), nan(size(a(index)))], [b(index), nan(size(b(index)))],'s','Color','k','MarkerSize',10,'DisplayName',data4)
% need vertica concatenation
plot([a(a_index); nan], [b(a_index); nan],'s','Color','k','MarkerSize',10,'DisplayName', 'data4')
hold off
legend()
  1 Kommentar
Mathan
Mathan am 4 Mai 2022
Bearbeitet: Mathan am 4 Mai 2022
Thank you - exactly which I was looking for! Is it possible to show the legends for 'sin' and 'cos' separately from the legends of 'data1,' 'data2', 'data3', 'data4' (like two different legend boxes)?
I tried:
a=(1:1:9)';
a=reshape(a,3,[]);
b=sin(a);
c=cos(a);
sc1=scatter(a,b,'.','DisplayName', 'sin')
hold on
sc2=scatter(a,c,'.','DisplayName','cos')
data1='Data1';
pl1=plot(a(2),b(2),'o','Color','r', 'MarkerSize',5,'DisplayName',data1)
data2='Data2';
pl2=plot(a(4),b(4),'o','Color','r','MarkerSize',10,'DisplayName',data2)
data3='Data3';
pl3=plot(a(6),b(6),'s','Color','k', 'MarkerSize',5,'DisplayName',data3)
data4='Data4';
index = find(a>=3 & a<=7);
pl4=plot([a(index); nan(size(a(index)))], [b(index); nan(size(b(index)))],'s','Color','k','MarkerSize',10,'DisplayName',data4)
% plot([a(a_index),nan], [b(a_index),nan],'s','Color','k','MarkerSize',10,'DisplayName',data4)
hold off
lg1=legend([sc1 sc2])
lg2=legend([pl1 pl2 pl3 pl4])
But the first legend (i.e. lg1) is not visible.
Thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jonas
Jonas am 4 Mai 2022
I am not 100% sure what you want to do: if I understood correctly, you want to display a legend also for data which was not plotted or which was NaN
you can circumvent the legend behavior by plotting NaN values which are not not visually displayed, but are recognized by the legend. you can then create the legend and set the auto update function to off
figure;
% plot NaN values for legend entries
plot(NaN,NaN,'DisplayName','label1'); hold on;
plot(NaN,NaN,'DisplayName','label2');
plot(NaN,NaN,'DisplayName','label3');
plot(NaN,NaN,'DisplayName','label4');
lg=legend(); % create legend
lg.AutoUpdate='off'; % disable auto update
set(gca,"ColorOrderIndex",1) % reset the default coloring to 1 to start the colors like for the NaN values
plot(1:20);
plot(nan(1,15));
plot(3:5);
  1 Kommentar
Mathan
Mathan am 4 Mai 2022
Thank you - a new information for me! I was actually able to display the legend entries whenever the values were NaN but some of them were duplicating (as shown in the attached image). I wanted to get rid of the duplicate.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by