Remove legend items from Figure

조회 수: 100 (최근 30일)
shdotcom shdotcom
shdotcom shdotcom 2019년 1월 25일
댓글: shdotcom shdotcom 2019년 1월 25일
In the figure, how to remove the item "aa" from the legennd without changing the shape of other items?
PointStyle = {'kp-', ...
'LineWidth', 1,...
'MarkerEdgeColor', 'k',...
'MarkerFaceColor', [0 0 .8],...
'MarkerSize',9};
plot(Points, PointStyle{:});
hold on
dataStyle = {'Marker','o', ...
'LineWidth', 1,...
'MarkerEdgeColor', [.8 0.4 0.2],...
'MarkerFaceColor', [1 .6 0.2],...
'MarkerSize',7};
plot(data, dataStyle{:});
if n <=3
%toDO
else
legend_str = {'aa','bb','cc'};
legend(legend_str, 'Fontsize',10,'Location', 'southoutside', 'Orientation','horizontal');
end
if n <=3
%toDO
else
legend_str = {'aa','bb','cc'};
legend(legend_str{2}, legend_str{3}, 'Fontsize',10,'Location', 'southoutside', 'Orientation','horizontal');
end
The shape of third item "cc" is changed:
  댓글 수: 3
shdotcom shdotcom
shdotcom shdotcom 2019년 1월 25일
편집: shdotcom shdotcom 2019년 1월 25일
@madhan ravi Is it possible to do this without using figure handle?
Also, in my case , the dimensions of plots are not consistent.
I have edited the question
madhan ravi
madhan ravi 2019년 1월 25일
Ok see this example , see if this is what you want:
plot(1:10,'-*r')
hold on
plot(2*(1:10),'-vg')
plot(3*(1:10),'-ob')
h=get(gca,'Children'); % grab all the axes handles at once
legendstr={'aa','bb','cc'};
legend(h([1 3]),legendstr{[1 3]}) % see the second label is skipped withoud changing shape
hold off

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

채택된 답변

Lucademicus
Lucademicus 2019년 1월 25일
You could do it based on the order of appearance of the plots.
Assuming 'aa' is plot first, then 'bb' and then 'cc':
figure;
hold on
plot(rand(4,1),rand(4,1),'r','DisplayName','Red')
plot(rand(4,1),rand(4,1),'g','DisplayName','Green')
plot(rand(4,1),rand(4,1),'b','DisplayName','Blue')
hPlots = flip(findall(gcf,'Type','Line')); % flipped, because the lines our found in reverse order of appearance.
legend_str = {'aa','bb','cc'};
legend(hPlots(2:3), legend_str(2:3), 'Fontsize',10,'Location', 'southoutside', 'Orientation','horizontal');
  댓글 수: 1
shdotcom shdotcom
shdotcom shdotcom 2019년 1월 25일
It is working!. Thank you. But, I could not undestand why my code is working when data size is [10,100] and it does not, when data size [5,200].

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by