Why legend is not displayed?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
How can I display the legend at my desired side outside the figure? I run the following code but it doesn't work:
clear all; close all; clc
one=rand(100,1);
fitness2sn0=one';
one=rand(100,1);
fitness2sn5=one';
one=rand(100,1);
fitness2sn10=one';
one=rand(100,1);
fitness2sn15=one';
fitness2sn0=sort(fitness2sn0,'descend');
fitness2sn5=sort(fitness2sn5,'descend');
fitness2sn10=sort(fitness2sn10,'descend');
fitness2sn15=sort(fitness2sn15,'descend');
h1=boxplot([fitness2sn0; fitness2sn5; fitness2sn10; fitness2sn15].');
set(h1,{'linew'},{2})
grid
Ax = gca;
Ax.YLabel.String = '\bf fitness';
Ax.YScale = 'log';
Ax.XTickLabel = compose('2sn%d',[35 45 55 65]);
Ax.YLim = [1E-3 1e0];
title('\bf Fitness')
set(gca,'linew',2)
lgd = legend('35dB', '45dB','55dB','65dB')
lgd.Location = 'southoutside';
채택된 답변
Simon Chan
2023년 2월 5일
You may refer to this answer https://www.mathworks.com/matlabcentral/answers/127195-how-do-i-add-a-legend-to-a-boxplot-in-matlab
and do the modification like the following:
one=rand(100,1);
fitness2sn0=one';
one=rand(100,1);
fitness2sn5=one';
one=rand(100,1);
fitness2sn10=one';
one=rand(100,1);
fitness2sn15=one';
fitness2sn0=sort(fitness2sn0,'descend');
fitness2sn5=sort(fitness2sn5,'descend');
fitness2sn10=sort(fitness2sn10,'descend');
fitness2sn15=sort(fitness2sn15,'descend');
colors = [1 0 0; 0 0.5 0; 0.5 0 0; 0 0 0.5]; % Add different color
h1=boxplot([fitness2sn0; fitness2sn5; fitness2sn10; fitness2sn15]','Colors',colors);
set(h1,{'linew'},{2})
grid
Ax = gca;
Ax.YLabel.String = '\bf fitness';
Ax.YScale = 'log';
Ax.XTickLabel = compose('2sn%d',[35 45 55 65]);
Ax.YLim = [1E-3 1e0];
title('\bf Fitness')
set(gca,'linew',2)
hChildren = findall(gca,'Tag','Box');
lgd = legend(hChildren([4 3 2 1]), {'35dB','45dB','55dB','65dB'});
lgd.Location = 'southoutside';

댓글 수: 11
Sadiq Akbar
2023년 2월 5일
Thanks for your kind response dear Simon Chan. But how can we add the following colors instead of those you have given.
colors=['blue', 'red', 'yellow', 'violet'];
Try this:
colors = [0,0,1; 1,0,0; 1,1,0; 238/255,130/255,238/255];
Sadiq Akbar
2023년 2월 5일
편집: Sadiq Akbar
2023년 2월 5일
Thanks for your kind response dear Simon Chan. But I want colours like this as shown in the attachment.
colors = [0,0.447,0.741; 0.85,0.325,0.098; 0.929,0.694,0.125; 0.494 0.184 0.556];
Actually you can retrieve the colors as follow:
h = openfig('desiredColors.fig');
hLine = findobj(h.Children,'Type','Line');
colors = flipud(cat(1,hLine.Color)); % Flip up down since the color is reversed
Sadiq Akbar
2023년 2월 5일
Thanks for your kind response dear Simon Chan. Actually these colours in the attachment are the default colors of MATLAB. I don't want to use this figure for color extraction. I want that MATLAB itself assigns its default color like the one shown in the attachment. In other graphs, MATLAB has assigned its default colors like in the attachment but here in this code, MATLAB doesn't assign its default colors like this. Why? And how can we invoke that?
Simon Chan
2023년 2월 5일
I don't have any idea about the default colors in different MATLAB graphics. Sorry about that.
Sadiq Akbar
2023년 2월 5일
The 'lines' colormap gives the default colors used by objects using the 'colororder' property of the axes. So you could use:
colors = lines(4);
If your inputs were in an array of some sort, the argument (i.e. 4) could also be defined programmatically, but the way they're given as separate named variables, there isn't a practical way to know how many things you intend to pass to boxplot() before you do.
Sadiq Akbar
2023년 2월 5일
Thanks a lot dear DGM for your kind response. Indeed it works. But how can I accept your answer also or if not possible then how can I vote you as there is no 'vote' icon?
DGM
2023년 2월 5일
Simon's answer is the core of any solution to this problem, so it's appropriate that he gets credit. While it's sometimes unfortunate that comments can't be upvoted for emphasis, your comment is verification that it helped. That much is sufficient for my needs, and it should help any future readers with the same problem.
Sadiq Akbar
2023년 2월 5일
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
