How to manually arrange the Legend strings
18 ビュー (過去 30 日間)
古いコメントを表示
I have plot in which it consists of 9 lines.I want to describe all these lines step by step for example it should display the red line first with 3 variable case and similarly for remaining two colors.
0 件のコメント
回答 (2 件)
Brendan Hamm
2015 年 8 月 10 日
With 3 variable case, what does this mean? I'm going out on a whim and assuming you just want to change the order of the lines.
By default the order of the lines in the legend is the same as they are plotted. The easiest way is to just plot them in the order you want the legend to appear.
If that is not feasible, then you can change the order of the lines as they are stored in the 'Children' property of the axes. This defines the order they are listed in the legend (except they are flipped so that the first element in the list is the last legend entry).
% Make some data to plot
x = 0:0.01:1;
y = randn(3,length(x));
ax = axes;
hold all % Allows color to be cycled (even prior to 2014b)
% Plot the data
for k = 1:3
plot(x,y(k,:))
end
legend('A','B','C')
% In 2015a this is blue, red, yellow
myLines = get(ax,'Children');
% Change the order the data is plotted.
set(ax,'Children',myLines([3 1 2]));
legend('A','B','C')
% Now it is red, blue , yellow
2 件のコメント
Arthur Vieira
2021 年 6 月 16 日
編集済み: Arthur Vieira
2021 年 6 月 16 日
But that switches the order the plotted lines. I need to switch the order of the legends, but keep the same plot order. In my case I'm plotting two fill()s and the two plot()s. The fills represent the standard deviation and the plots the mean. So naturally the plots need to be on top of the fills [fill1 fill2 plot1 plot2], but the legends should be ordered [plot1 fill1 plot2 fill2] in order to make sense. Is there really no way but to photoshop it later? xD
Arthur Vieira
2021 年 6 月 16 日
Found the solution here:
https://se.mathworks.com/matlabcentral/answers/244707-how-to-change-order-of-legends
Govind K
2015 年 8 月 10 日
1 件のコメント
Marya Sadki
2021 年 11 月 29 日
Hi, are you find a solution for this?
If yes, please tell me i have the same problem
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!