Showing legend even if data is not there

20 views (last 30 days)
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.

Accepted Answer

Chunru
Chunru on 4 May 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 Comment
Mathan
Mathan on 4 May 2022
Edited: Mathan on 4 May 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

Sign in to comment.

More Answers (1)

Jonas
Jonas on 4 May 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 Comment
Mathan
Mathan on 4 May 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!