How to manually arrange the Legend strings
18 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Brendan Hamm
on 10 Aug 2015
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 Comments
Arthur Vieira
on 16 Jun 2021
Edited: Arthur Vieira
on 16 Jun 2021
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
on 16 Jun 2021
Found the solution here:
https://se.mathworks.com/matlabcentral/answers/244707-how-to-change-order-of-legends
Govind K
on 10 Aug 2015
1 Comment
Marya Sadki
on 29 Nov 2021
Hi, are you find a solution for this?
If yes, please tell me i have the same problem
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!