Why legend is not displayed?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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'];
Simon Chan
2023 年 2 月 5 日
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.
Simon Chan
2023 年 2 月 5 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
