how to change order of legends?
Mostra commenti meno recenti
I dont want to change the order of my plots, but I want to change the order in the legend list. How to do?
Risposta accettata
Più risposte (10)
I think Mr. M means the following: In the figure GUI, after one has worked hard to generate a nice figure by combining the outputs of various scripts via copy + paste, how can one change the order of the legend?
In earlier versions of MATLAB, one could achieve this by cutting and repasting traces. They would then reappear at the bottom of the legend. So you would take the trace you want to be second, cut and paste it, then do the same for the third, all the way to the Nth, and then the legend would have the desired order. Now it is no longer possible to do this. The figure somehow remembers the position of the trace before it was cut.
But there is a WORK-AROUND! Simply cut the trace, paste it in onto a different axis of a different figure, then cut that trace and paste it back onto the original figure. This stymies matlab's attempt to remember the order of the trace and puts it onto the bottom of the legend.
6 Commenti
Navid Naderializadeh
il 7 Mar 2018
Great answer! Actually you don't need a separate figure to paste; just click and select the curves in the order you want, then press Ctrl+X and then immediately Ctrl+V.
sarmad a
il 15 Mar 2018
Spostato: Dyuman Joshi
il 18 Set 2023
@David: you really saved my time !! Thanks
Jeremy Cribb
il 15 Mar 2018
Hi, Navid. Your suggestion works for older versions of matlab, in my experience up to 2014b. I haven't used versions between R2014b and R2017b. David's strategy was required for me to re-order in v2017b.
Mr M.
il 20 Mar 2018
Walter Roberson
il 19 Ago 2019
Modificato: Walter Roberson
il 15 Gen 2021
fariborz eshraghi
il 5 Dic 2021
That's a wonderful help, thanks.
You can specify the handles in a particular order when you create the legend.
% Sample data
x = 0:360;
y1 = sind(x);
y2 = cosd(x);
y3 = tand(x);
% Set up axes
axis([0 360 -1 1])
hold on
% Create plots
h = gobjects(3, 1);
h(1) = plot(x, y1, 'DisplayName', 'sine');
h(2) = plot(x, y2, 'DisplayName', 'cosine');
h(3) = plot(x, y3, 'DisplayName', 'tangent');
% Create legend -- cosine first then sine and finally tangent
legend(h([2 1 3]))
Rick
il 24 Mar 2018
use this code to swap the order of two plots:
plots=get(gca, 'Children');
legend(plots(2, 1), {'Second line', 'First line'});
5 Commenti
Walter Roberson
il 21 Ago 2018
You probably mean
legend(plots([2, 1]), {'Second line', 'First line'});
Gopinath Karuppannan
il 29 Ott 2021
By using the above code, the order of the legends (only strings) are varying but the colors are not varied.
Just a note that if you plot 2 plots in the same figure, plots([1]) is the last plot (the 2nd one) that you plotted, and plots([2]) is the first plot that you plotted, at least in 2020B version. Be careful to maintain the correct legend name for the correct plot.
So if you wanted to switch the order of legend entries from the default, you would write (supposing you have 3 plots for clarity of example):
figure();
plot(first_x, first_y);
hold on;
plot(second_x, second_y);
plot(third_x, third_y);
plots=get(gca, 'Children');
legend(plots([1, 2, 3]), {'third line', 'second line', 'first line'})
Walter Roberson
il 28 Set 2023
It is not clear what plots is in your code? You are using it as if it is a function or a multidimensional array.
@Walter Roberson Thanks for the catch. I've updated my comment with a line to define plots as it was used above (and fixed the same typo that Rick had)
a = linspace(1,10,10);
b1 = sin(a);
Plot1 = plot(a,b1);
hold on
b2 = cos(a);
Plot2 = plot(a,b2);
hold off
legend([plot2 plot1],{‘Second’,’First’})
2 Commenti
Jim Bosley
il 19 Ago 2019
Last line needs plot handles capitalized, as they were defined.
a = linspace(1,10,10);
b1 = sin(a);
Plot1 = plot(a,b1);
hold on
b2 = cos(a);
Plot2 = plot(a,b2);
hold off
legend([Plot2 Plot1],{‘Second’,’First’})
MCH
il 16 Dic 2020
@Ed: This solution works great
Liwei Wang
il 25 Gen 2019
Cut+paste is a good idea. But sometimes it won't work because the plots are pretty close to each other. An alternative approach is to change the order of the plots by code:
Here is the copy of the code:
figure
scatter(rand(150,1),rand(150,1))
hold on
fill([0.2 0.5 0.5 0.2],[0.2 0.2 0.5 0.5],'r')
hg = line([0 0.6],[0.6 0]);
set(hg,'LineWidth',12,'Color','g')
h = get(gca,'Children');
set(gca,'Children',[h(3) h(2) h(1)])
Refresh legend then you will see the change.
1 Commento
Giorgia Calisti
il 9 Giu 2020
Exactly what I was looking for!
Thank you
Kris Govertsen
il 15 Gen 2021
This is how I was able to change the order of the legend on a figure with multiple subplots of area plots:
Before:

I want the order of the legend to follow the order of the colors in the area plot
% a is my figure
% If I type
If I type the following into the command window: a.Children... it returns:
% a.Children
%
% ans =
%
% 5×1 graphics array:
%
% Legend (Grid, VRFB error, VRFB Power, VRFB Energy, LIB error, LIB Power, LIB Energy, Solar, Tidal)
% Axes (Tidal RES)
% Axes (Solar PV RES)
% Axes (VRFB Cost)
% Axes (LIB Cost)
So a.Children(1) is my legend!
% Re-order Legend
lbl = a.Children(1).String; % Retrieve legend labels
numlbl = length(lbl); % Determine number of lables
order = sort(1:1:numlbl,'descend'); % Create array of label numbers in descending order
newlbl = lbl(order); % Create new labels in descending order
legend(findobj(a.Children(2),'Type','area'),newlbl) % Set the legend to follow the new labels

hope this helps!
2 Commenti
Jan Heinsoth
il 22 Feb 2021
Nice, someone wanted to do the same as me. Thanks
LIU
il 22 Mar 2022
Hi, I have a silimar problem, and guess your method could be useful for me. But I don't know how to apply your code suited to my case. Would you please kindly have a look at my problem and give me a hand? Thank you very much.
The link is :
FM
il 22 Mag 2018
1 voto
There is a good solution in the `Update` section of this post on Stack Overflow: http://stackoverflow.com/a/39104494/3230708
George
il 23 Set 2015
Is this something you are after, the question is not clear
x=magic(2);
plot(x(:,1));
hold
plot(x(:,2),'r');
legend('Data1','Data2');
%blue = Data1
%red=Data2
to change that simple reverse the string order
legend('Data2','Data1');
see also this http://nl.mathworks.com/help/matlab/ref/legend.html and this http://nl.mathworks.com/help/matlab/ref/legend-properties.html For more information on handling the legends
Kris Govertsen
il 15 Gen 2021
This is how I was able to change the order of the legend on a figure with multiple subplots of area plots:
Before:

I want the order of the legend to follow the order of the colors in the area plot
% a is my figure
% If I type
If I type the following into the command window: a.Children... it returns:
% a.Children
%
% ans =
%
% 5×1 graphics array:
%
% Legend (Grid, VRFB error, VRFB Power, VRFB Energy, LIB error, LIB Power, LIB Energy, Solar, Tidal)
% Axes (Tidal RES)
% Axes (Solar PV RES)
% Axes (VRFB Cost)
% Axes (LIB Cost)
So a.Children(1) is my legend!
% Re-order Legend
lbl = a.Children(1).String; % Retrieve legend labels
numlbl = length(lbl); % Determine number of lables
order = sort(1:1:numlbl,'descend'); % Create array of label numbers in descending order
newlbl = lbl(order); % Create new labels in descending order
legend(findobj(a.Children(2),'Type','area'),newlbl) % Set the legend to follow the new labels

hope this helps!
Sandeep A S V
il 7 Gen 2022
Modificato: Sandeep A S V
il 7 Gen 2022
0 voti

There is a simple solution using the property inspector GUI.
1.Go to Edit menu on the figure window
2. Select 'current object properties'
3. Select one of the plots in your figure
4. Change the property 'SeriesIndex' in COLOR AND STYLING tab.
1 Commento
Muhammad Tauha Ali
il 7 Apr 2024
In 2022b, order can be interactively changed by moving the object order through mouse and updating the series index. The legend will need to be regenerated after the changes though.

Categorie
Scopri di più su Legend in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







