Showing legend even if data is not there

27 次查看(过去 30 天)
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.

采纳的回答

Chunru
Chunru 2022-5-4
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 个评论
Mathan
Mathan 2022-5-4
编辑:Mathan 2022-5-4
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

请先登录,再进行评论。

更多回答(1 个)

Jonas
Jonas 2022-5-4
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 个评论
Mathan
Mathan 2022-5-4
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by