how to get only selected legend in this particular case
조회 수: 5 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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'})))
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!