How to change legend entries' order with addaxis function?

5 views (last 30 days)
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)

DGM
DGM on 22 Mar 2022
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
DGM
DGM on 22 Mar 2022
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.
LIU
LIU on 23 Mar 2022
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.

Community Treasure Hunt

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

Start Hunting!