How to reorder legend entries with plot children

조회 수: 75 (최근 30일)
Enoch23
Enoch23 2019년 10월 29일
댓글: Ipek Gokulu 2023년 9월 16일
Hello,
I have a loop creating some plots and on some of them I would like to change the order of the legend entries. I came across a method on StackOverflow, however, it doens't seem to work.
I tried the example in excaza 's answer without success.
When I type in lh.PlotChildren(neworder) I get
>> lh.PlotChildren(neworder)
ans =
4×1 Line array:
Line (y = 3*x)
Line (y = x)
Line (y = x.^2)
Line (y = 2*x)
which is the correct order. However, when assigning
>> lh.PlotChildren = lh.PlotChildren(neworder)
lh =
Legend (y = x, y = 2*x, y = 3*x, y = x.^2) with properties:
String: {'y = x' 'y = 2*x' 'y = 3*x' 'y = x.^2'}
Location: 'northeast'
Orientation: 'vertical'
FontSize: 9
Position: [0.7274 0.7282 0.1589 0.1726]
Units: 'normalized'
Show all properties
it doesn't work. Everything stays as is.
Am I missing something?
  댓글 수: 1
Kyle Marquis
Kyle Marquis 2020년 9월 19일
Also having the same problems, and the solution given by Sebastian Bomberg is not helpful. Did it work for you?Anyone else have a solution?

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

답변 (4개)

Sebastian Bomberg
Sebastian Bomberg 2019년 10월 29일
You can reorder the children of the axes:
ax = gca;
ax.Children = ax.Children(neworder);
  댓글 수: 1
Kyle Marquis
Kyle Marquis 2020년 9월 19일
I am having the same issues as Enoch23, and your "solution" has not helped. Are you able to show how this re-orders the legend as asked in the question?

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


Kyle Marquis
Kyle Marquis 2020년 9월 19일
편집: Kyle Marquis 2020년 9월 19일
I found a solution that can be used to re-order legend entries without messing up the order in which they are plotted on top of each other (But it doesn't involve plot children). I found it from https://matplotlib.org/1.3.1/users/legend_guide.html , and it's really simple, all you need to do is call
legend([p2, p1], ["line 2", "line 1"])
with p1 being the line object created when you plot
p1 = plot(...)
and together with uistack, I am able to change which objects get plotted on top of which, but then reorder the legend so it makes sense. Example
uistack(psave_d,'top') % Brings certain line to front
legend([psave_a, psave_b, psave_g, psave_c, psave_d, psave_e, psave_f, psave_pde], ["k_y=0.000001 W/m/K","k_y=0.0001 W/m/K","k_y=0.001 W/m/K","k_y=0.01 W/m/K","k_y=0.1 W/m/K","k_y=1 W/m/K","k_y=10 W/m/K","Isothermal PDE Numerical Model"])
If anyone needs more detail, I can gladly provide it. Cheers

nt_ba
nt_ba 2022년 6월 10일
I found the solution. Worked perfect for me!
So after you open the .fig file, point at the plot (or subplot) you want to make the change on the legends order.
Then,
ax = gca;
ax.Children % Here you will see the order of your legends. Suppose you have 5 different legends
ax.Children = [ax.Children(2) ax.Children(3) ax.Children(1) ax.Children(4) ax.Children(5)]; % Indicatively, this is how to reorder the legends
Don't forget after doing the aforementioned changes to delete the legend in the figure and insert it again.
Cheers!
  댓글 수: 1
Ipek Gokulu
Ipek Gokulu 2023년 9월 16일
I think this is the most convenient way to do it especially if you are working on a previously saved figure. Thank you!

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


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!

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by