skip legend entries while plotting data

12 vues (au cours des 30 derniers jours)
michael
michael le 10 Oct 2017
I have following issue:
I'm plotting multiple graphs on the same figure. Before plotting them, there are some data interpolation. It can be that some of the data is empty set. the plotting command is like that:
plot(Ax, Ay, 'bo', Bx, By, 'go', Cx, Cy, r*)
legend('A', 'B', 'C')
When set A (Ax, Ay), for example, is empty (and there is data in B & C sets), in the generated label, it will associate set A to green color, B to red color and it will not display the C legend.
How to solve the issue that if there is empty set, it will skip it in the legend?
Thank you!

Réponses (3)

Rik
Rik le 10 Oct 2017
Modifié(e) : Rik le 10 Oct 2017
I use multiple calls to plot, so I can get a list of handles, which you can then use in the call to legend
h=[];
h(1)=plot(rand(2));hold on
h(2)=plot(0:0.1:1);
legend(h,{'A','B'})
edit: don't forget hold on (which I tend to do often, apparently even in answering here)

sandeep singh chauhan
sandeep singh chauhan le 1 Août 2018
Suppose I have a vector A1 and B1 denotes its corresponding legends and I want to skip the legends for zeros in A1 means I don't want legend 'D','G','H','I'
A1 = [ 1 2 3 0 4 5 0 0 0 8]; B1 = {'A','B','C','D','E','F','G','H','I','J'}; D1 = []; for i =1:length(A1) if (A1(i) ~=0) C1 = i; plot(A1(i):A1(i)+20); %% example for plot D1 = [D1;strcat(B1(C1))]; legend(D1); hold on end
end

sandeep singh chauhan
sandeep singh chauhan le 12 Août 2018
A1 = [ 1 2 3 0 4 5 0 0 0 8]; B1 = {'A','B','C','D','E','F','G','H','I','J'}; D1 = []; for i =1:length(A1) if (A1(i) ~=0) C1 = i; plot(A1(i):A1(i)+20); %% example for plot D1 = [D1;strcat(B1(C1))]; legend(D1); hold on end
end

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by