Info

この質問は閉じられています。 編集または回答するには再度開いてください。

I have problems with multi legends

2 ビュー (過去 30 日間)
Korhan Cengiz
Korhan Cengiz 2015 年 11 月 11 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Can you give some idea for my problem like this:
for plot legend end
then
if (main)
if 1
plot
legend
if 2
plot
legend
...
if n
plot
legend
end
end
for each legend I want to renew the legend box and I want to add it to box and I want to update and enlarge the box.
Thanks

回答 (1 件)

dpb
dpb 2015 年 11 月 11 日
編集済み: dpb 2015 年 11 月 11 日
First, save the handle to the legend on creation. Then, also save the line handles created by plot each time in an array and augment the handle 'String' property associating the the cell array of titles with the line plot handles...
hL=zeros(N,1); % for line handles
hL(1)=plot(...; % first line
lab{1,1}=num2str(1,'Line %2d'); % first label
hLg=legend(lab); % create the legend object, first label
hold on % to add onto existing...
for i=2:N % subsequent after first preparation
hL(i)=plot(...; % next line
lab{i,1}=num2str(i,'Line %2d'); % and next label add to cell array
legend(hL,lab); % apply the current labels to the existing line handles..
end
Alternatively, of course, you can build the array of data as a 2D array and wait and plot all in "one swell foop" and then legend can automagically do the association of an array of labels one:one with the lines (columns) in the array. But, the above is the pattern to do it a line at a time which has its reasons for doing so, granted...
  2 件のコメント
Korhan Cengiz
Korhan Cengiz 2015 年 11 月 11 日
thanks your reply but can you send me example with usage of if clauses.
dpb
dpb 2015 年 11 月 11 日
What about if? There are examples at
doc if
The issue you outlined above re: adding to a legend has to do with keeping track of the handles of the objects in the axes to be added to an existing legend, not if...that appears a distraction.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by