Showing legend even if data is not there
27 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuesta aceptada
Chunru
el 4 de Mayo de 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 comentario
Más respuestas (1)
Jonas
el 4 de Mayo de 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);
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!