필터 지우기
필터 지우기

Legend in for loop

조회 수: 4 (최근 30일)
Mahesh
Mahesh 2015년 10월 23일
댓글: Mahesh 2015년 10월 23일
Dear all, I would like to insert legend inside the for loop plot. In this plot same class repeated many times. I want to display one categorry at a time.
for i = 1:udClass
Legend{i} = ['Class' int2str(i)];
end
for i = 1:N
idClass = classVec(i);
plot(X(:,i), 'color', colors(idClass,:))
x = floor(ndata/2);
y = 1- (i-1)/N;
text(x, y, int2str(i), 'color', colors(idClass,:), 'VerticalAlignment', 'top',...
'HorizontalAlignment','center','FontSize',8)
end
legend(Legend, 'Location', 'Southeast');
With this code I got like shown
in which I got color repeated for different classes but I want each class with assigned colors. Could you please help me in this regard. Thanks

채택된 답변

Kelly Kearney
Kelly Kearney 2015년 10월 23일
Save the handles of your plotted lines, then pass the specific handles of lines with a unique class to the legend command:
for i = 1:udClass
Legend{i} = ['Class' int2str(i)];
end
h = gobjects(N,1); % h = zeros(N,1) if R2014a or earlier
for i = 1:N
idClass = classVec(i);
h(i) = plot(X(:,i), 'color', colors(idClass,:))
x = floor(ndata/2);
y = 1- (i-1)/N;
text(x, y, int2str(i), 'color', colors(idClass,:), 'VerticalAlignment', 'top',...
'HorizontalAlignment','center','FontSize',8)
end
[~,idx] = unique(classVec);
legend(h(idx), Legend, 'Location', 'Southeast');
  댓글 수: 2
Mahesh
Mahesh 2015년 10월 23일
Thanks Kelly But the handles may be different as udClass and N are not equal. Let me try..
Thanks
Mahesh
Mahesh
Mahesh 2015년 10월 23일
Thanks a lot Kelly!! It worked...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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