how to get only selected legend in this particular case

조회 수: 3 (최근 30일)
Megha
Megha 2019년 1월 6일
댓글: Megha 2019년 1월 6일
I have attached data, can anyone help me getting red, blue and green legends only?
yt = logspace(-3,4,8);
binranges = yt;
[bincounts] = histc((I(:,1)),binranges);
h1 = bar(binranges,bincounts,'histc','FaceColor',[1 0 0]);
set(h1,'FaceColor','r','EdgeColor','r');
hold on;
[bincounts] = histc(1./(D(:,1)),binranges);
h2 = bar(binranges,bincounts,'histc');
set(h2,'FaceColor','g','EdgeColor','g');
hold on;
[bincounts] = histc((NO(:,1)),binranges);
h3 = bar(binranges,bincounts,'histc');
set(h3,'FaceColor','b','EdgeColor','b');
legend('show');
set(gca,'XMinorTick','on','YMinorTick','on','XScale','log');
The above code shows 6 legends for 3 dataset.
Can anyone give me only colors and no * mark.

채택된 답변

Rik
Rik 2019년 1월 6일
편집: Rik 2019년 1월 6일
Using explicit handles to the patch objects will fix the legend for you. Also, you don't need the second call to hold on. The last part of the code will delete all line objects in the plot. If you want to change the legend labels, look into the doc for the legend function.
S=load('matlab.mat');D=S.D;I=S.I;NO=S.NO;
figure(1),clf(1)
yt = logspace(-3,4,8);
binranges = yt;
[bincounts] = histc((I(:,1)),binranges);
h1 = bar(binranges,bincounts,'histc');
set(h1,'FaceColor','r','EdgeColor','r');
hold on;
[bincounts] = histc(1./(D(:,1)),binranges);
h2 = bar(binranges,bincounts,'histc');
set(h2,'FaceColor','g','EdgeColor','g');
%hold on;%redundant, as the NextPlot property is already set
[bincounts] = histc((NO(:,1)),binranges);
h3 = bar(binranges,bincounts,'histc');
set(h3,'FaceColor','b','EdgeColor','b');
legend([h1 h2 h3]);%use handles to patch objects
set(gca,'XMinorTick','on','YMinorTick','on','XScale','log');
children=get(gca,'Children');
types=get(children,'Type');
delete(children(ismember(types,{'line'})))
  댓글 수: 2
Megha
Megha 2019년 1월 6일
Wow....!!
I was struggling since more than a week time...
Megha
Megha 2019년 1월 6일
Thnak you so much

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

추가 답변 (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