how to change order of legends?

조회 수: 890(최근 30일)
Mr M.
Mr M. 2015년 9월 23일
편집: Sandeep A S V 2022년 1월 7일 1:22
I dont want to change the order of my plots, but I want to change the order in the legend list. How to do?

답변(11개)

David
David 2018년 1월 10일
편집: David 2018년 1월 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.
  댓글 수: 5
fariborz eshraghi
fariborz eshraghi 2021년 12월 5일
That's a wonderful help, thanks.

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


Rick
Rick 2018년 3월 24일
use this code to swap the order of two plots:
plots=get(gca, 'Children');
legend(plots(2, 1), {'Second line', 'First line'});
  댓글 수: 2
Gopinath Karuppannan
Gopinath Karuppannan 2021년 10월 29일
By using the above code, the order of the legends (only strings) are varying but the colors are not varied.

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


Liwei Wang
Liwei Wang 2019년 1월 25일
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:
answered by the cyclist.
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
Giorgia Calisti
Giorgia Calisti 2020년 6월 9일
Exactly what I was looking for!
Thank you

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


Ed Rawi
Ed Rawi 2018년 10월 22일
편집: Ed Rawi 2018년 10월 23일
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
MCH
MCH 2020년 12월 16일
@Ed: This solution works great

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


Kris Govertsen
Kris Govertsen 2021년 1월 15일
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!
  댓글 수: 1
Jan Heinsoth
Jan Heinsoth 2021년 2월 22일
Nice, someone wanted to do the same as me. Thanks

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


Steven Lord
Steven Lord 2021년 2월 22일
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]))

FM
FM 2018년 5월 22일
There is a good solution in the `Update` section of this post on Stack Overflow: http://stackoverflow.com/a/39104494/3230708

George
George 2015년 9월 23일
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');

sarmad a
sarmad a 2018년 3월 15일
@David: you really saved my time !! Thanks

Kris Govertsen
Kris Govertsen 2021년 1월 15일
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
Sandeep A S V 2022년 1월 7일 1:21
편집: Sandeep A S V 2022년 1월 7일 1:22
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.

Community Treasure Hunt

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

Start Hunting!

Translated by