How to change legend entries' order with addaxis function?

Hello everyone,
I have three lines subplotted in one figure by using the great addaxis tool. The codes run successfully and get the following figure:
Everything goes well except that the order of entries in legend is not what I what. I need the legend shows its entries in order, i.e.,starting from 1 and ending in 10 ,just like this picture(the linetype hasn't been changed, since the number is manually changed by me for purpose of demonstration):
The problem is I need to change the arguments' order displayed in legend. The codes just stopped somewhere if I tried to change the order by coding and get the following error hint:
Error using addaxis
Too many output arguments.
Error in plotyy1_all (line 4)
p2=addaxis(HOR,eval(['irf1.' var{1,jj},ending_cell{1,ii}]),'Linestyle','--','Color','r');
I just modified the original well-done codes in part as follows:
for ii=1:length(ending_cell)
HOR=1:1:10;
var={'b','l','l','l','l','l','l',};
var1={'b_t','l_t','l_t','l_t','l_t','l_t','l_t',};
figure('Name',strcat(ending_cell{1,ii},'1'))
for jj=1:length(var)
subaxis(3,3,jj,'MarginRight',0.3,'MarginLeft',0.3,'SpacingHoriz',0.1,'SpacingVert',0.1)
eval(['irf5.' var{1,jj},ending_cell{1,ii}]);
eval(['irf0.' var{1,jj},ending_cell{1,ii}]);
eval(['irf1.' var{1,jj},ending_cell{1,ii}]);
hold on
p1=plot(HOR,eval(['irf5.' var{1,jj},ending_cell{1,ii}]),'Linestyle','-','Color','k');
p2=addaxis(HOR,eval(['irf10.' var{1,jj},ending_cell{1,ii}]),'Linestyle','--','Color','r');
p3=addaxis(HOR,eval(['irf1.' var{1,jj},ending_cell{1,ii}]),'Linestyle','-.','Color','b');
p2_child = get(p2,'Children');
p3_child = get(p3,'Children');
title([var1{1,jj}])
end
ylabels = {'u=5', 'u=10', 'u=1'};
s1L=legend([p3_child p1 p2_child],ylabels,'Orientation','horizontal','Location','SouthOutside','NumColumns',3);
s1P = [0.5 0.02 0.03 0.03];
set(s1L,'Position', s1P,'Units', 'normalized');
end
Then I get error hint above.Could anyone kindly help me solve this problem?Thank you very much.
Best regards,
LIU

Answers (1)

Try something like this example:
N = 10;
HOR = 1:N;
D = rand(3,N);
for jj=1:9
subaxis(3,3,jj,'MarginRight',0.3,'MarginLeft',0.3,'SpacingHoriz',0.1,'SpacingVert',0.1)
hold on
plot(HOR,D(1,:),'Linestyle','-','Color','k');
addaxis(HOR,D(2,:),'Linestyle','--','Color','r');
addaxis(HOR,D(3,:),'Linestyle','-.','Color','b');
title('this is a title')
end
% get all children of the current axes (three line objects)
% since they're listed in order of ascending age, flip the vector
hl = flipud(get(gca,'children'));
% label vector in the same order
ylabels = {'u=1', 'u=5', 'u=10'};
s1L=legend(hl,ylabels,'Orientation','horizontal','Location','SouthOutside','NumColumns',3);
s1P = [0.5 0.02 0.03 0.03];
set(s1L,'Position', s1P,'Units', 'normalized');
There are probably other ways to go about it, but this is simple.

4 Comments

Thank you for your quick response, DGM. I'm sorry maybe I didn't express what I mean clearly, with your suggestion I get the following figure, though the number in the legend changes, the line itself seems unchanged in the legend.
Please let me put it more clear with your sample code. Because the addaxis tool's rule of adding axis is to add axis on the right side of the figure at first, and then the second axis added on the left side, and the third one on th right again, so I need to subplot the three lines in the order as follows so that the blue line's y axis are on the most left of the figure, then black line's , and finally red line's on the right.
plot(HOR,D(2,:),'Linestyle','-','Color','k');
addaxis(HOR,D(3,:),'Linestyle','--','Color','r');
addaxis(HOR,D(1,:),'Linestyle','-.','Color','b');
In this way, I get the legend's entries order: black line,red line,blue line. But I need the entries in the same order as what looks in the figure,i.e., from left to right: blue line,black line,red line.Could you please give me more suggestions about realizing this? Many thanks.
Best,
LIU
I find a useful link which may help me resolve this problem, but I don't understand how to apply it to my case.
Could anyone help have a look at that link:
Oh. Maybe I'm still misunderstanding the intent, but is something like this more like it?
N = 10;
HOR = 1:N;
D = rand(3,N).*[1;5;10]; % each series has increasing range
for jj=1:9
subaxis(3,3,jj,'MarginRight',0.3,'MarginLeft',0.3,'SpacingHoriz',0.1,'SpacingVert',0.1)
hold on
plot(HOR,D(1,:),'Linestyle','-','Color','k');
addaxis(HOR,D(2,:),'Linestyle','--','Color','r');
addaxis(HOR,D(3,:),'Linestyle','-.','Color','b');
title('this is a title')
end
% get all children of the current axes (three line objects)
% since they're listed in order of ascending age, flip the vector
hl = flipud(get(gca,'children'));
% label vector in the same order
ylabels = {'u=1', 'u=5', 'u=10'};
% list the legend entries in an arbitrary order
idx = [3 1 2];
s1L = legend(hl(idx),ylabels(idx),'Orientation','horizontal','Location','SouthOutside','NumColumns',3);
s1P = [0.5 0.02 0.03 0.03];
set(s1L,'Position', s1P,'Units', 'normalized');
That should allow you to just reorder the legend entries regardless of the order in which they're plotted.
Wow, thank you so much, DGM.That's it. By adding idx handle and change some order of the yalabels' entries, the figure does show as what I need. Thanks again.
Best regards,
LIU

Sign in to comment.

Asked:

LIU
on 22 Mar 2022

Commented:

LIU
on 23 Mar 2022

Community Treasure Hunt

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

Start Hunting!