How to reorder legend items individually in plotyy in a subplot
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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
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.
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!