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
Simon Chan 2023년 2월 5일

0 개 추천

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
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
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
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
Simon Chan 2023년 2월 5일
I don't have any idea about the default colors in different MATLAB graphics. Sorry about that.
Sadiq Akbar
Sadiq Akbar 2023년 2월 5일
Its ok dear Simon Chan. You have done a lot. I am going to accept your answer.
DGM
DGM 2023년 2월 5일
편집: DGM 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
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
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
Sadiq Akbar 2023년 2월 5일
Thanks a lot for your kind words dear DGM. Well said.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

질문:

2023년 2월 5일

댓글:

2023년 2월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by