skip legend entries while plotting data

62 ビュー (過去 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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by