skip legend entries while plotting data

조회 수: 13 (최근 30일)
michael
michael 2017년 10월 10일
답변: sandeep singh chauhan 2018년 8월 12일
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!

답변 (3개)

Rik
Rik 2017년 10월 10일
편집: Rik 2017년 10월 10일
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 2018년 8월 1일
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 2018년 8월 12일
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

카테고리

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