How to reorder legend items individually in plotyy in a subplot

3 views (last 30 days)
Hi all, I have a 3 x 2 subplot with each subplot containing results of tests done at a certain stream on different days. However to show the results better I used plotyy so the data for some days are on the righthand y axis with others on the left hand axis. Now, I can easily make a legend for all of the days but matlab just puts the lefthand y-axis data labels first and then the righthand data labels. This means that although my labels are in date order for each individual y axis (and I found a useful function reorderLegend to do this while keeping the lines in the same order since sometimes they are out of order for clarity when plotting), I don't know if it is possible to order each label individually (i.e. mix up labels from different axes). I seem to be able to call each y axis label and order them but can you somehow get a handle for the combined legend? I would like to keep all the labels in the same legend. Many thanks for your help! Cat

Answers (1)

dpb
dpb on 6 Jul 2015
From
help legend
...
legend(H,string1,string2,string3, ...) puts a legend on the plot
containing the handles in the vector H using the specified strings as
labels for the corresponding handles.
....
legend(M), where M is a string matrix or cell array of strings, and
legend(H,M) where H is a vector of handles to lines and patches also
works.
Can't you simply save the line handles from plotyy (they'll be in the 2nd and 3rd optional returned arrays H1, H2 and list them in the order desired should cause the labels to show up in that order as well.
  2 Comments
Catriona
Catriona on 7 Jul 2015
Hi dpb,
Thank you for getting back to me so quickly. I have tried to add a handle as you suggested but this still isn't working, I can only get a handle for either the whole plot (in which case it only counts the data in the first axis) or one axes at a time (hline1 or hline2), whereas I have several lines on each axis in one plot. The code for the relevant plot is below
subplot(3,2,2);
S5_low_traces=[S5_157_2011 S5_163_2011 S5_211_2010 S5b_218_2010];
S5_high_traces=[S5_208_2011_av S5_255_2011];
[haxes,hline1,hline2]=plotyy(MinutesFromInjection,S5_high_traces, MinutesFromInjection,S5_low_traces);
legend('S5 270711','S5 120911','S5 060611','S5 120611','S5 300710','S5b 060810','Location','EastOutside');
title ('S5');
xlabel('Minutes after injection');
axes(haxes(1))
ylabel('c_d for 2707 (ppb)','color','k');
xlim([1 1000]);
ylim([0 2]);
set(haxes(1),'YTick',[0 0.4 0.8 1.2 1.6 2]);
set(haxes(1),'YTickLabel',{0 0.4 0.8 1.2 1.6 2});
axes(haxes(2))
ylabel('c_d (ppb)');
xlim([1 1000]);
ylim([0 0.6]);
set(haxes(2),'YTick',[0 0.12 0.24 0.36 0.48 0.6]);
set(haxes(2),'YTickLabel',{0 0.12 0.24 0.36 0.48 0.6});
hold on
M=~(isnan(MinutesFromInjection)|isnan(S5_211_2010));
[X]=plot(MinutesFromInjection(M),S5_211_2010(M),'-','color','r');
xlim([1 1000]);
ylim([0 0.6]);
hAnnotation = get(X,'Annotation');
hLegendEntry = get(hAnnotation','LegendInformation');
set(hLegendEntry,'IconDisplayStyle','off')
hold off
Ok so as you can see because two of the lines are on one axis I have to put their legend items before the other lines even though their dates (270711 and 120911) come after the other lines. They need to be plotted before the other lines for visual reasons though. I am sorry I probably just don't understand handles very well! Many thanks, Cat
dpb
dpb on 7 Jul 2015
Edited: dpb on 7 Jul 2015
I can't run your code because don't have the data for all the arrays in the two traces.
But, I'm saying I believe it you reorder the two handle arrays that you call hline[1|2] in the order in which you want the legend labels to be then call legend with that order the legend entries will be in the order of the associated handle in the reordered list.
You do have to keep track of which you want in which order in the legend vis a vis the order in which you plot them if you can't, for some reason, plot them in the order you want them labelled.
Here's a trivial example of what I mean as far as reordering and placing in that order--
>> [hAx,hL1,hL2]=plotyy(1:10,rand(2,10),1:4,rand(3,4)); % some lines
>> iL1=randperm(length(hL1)), iL2=randperm(length(hL2)); % mix up orders
iL1 =
2 1
iL2 =
2 1 3
>> hMix=[hL1(iL1) hL2(iL2)]; % put the handles in same order
>> legend(hMix,'L1 - 2','L1 - 1','L2 - 2','L2 - 1','L2 3- 3')
And, indeed, as surmised the legends are placed in the order in the list and are associated with the lines in that (randomized) order.
So, the upshot is, decide which order the lines are to be placed in the legend and arrange the labels in that order but be sure you've got the handles of the lines in consonance with that order when passing them to legend.
ADDENDUM
NB: Just because I only rearranged the line handles within the two arrays, don't think that's a restriction; you can "choose one from column A, then one from column B...", etc., at any arbitrary ordering you wish. All you needs must do is to keep the text labels in the same order of the line handles when you make the call. The handles are really nothing magic, they're just names by which Matlab can keep track of "who's who in the zoo" and you can refer to whichever elephant or lion or tiger or bear (oh my!) as needed.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!