Re-orders the legend entries (without modifying the actual order the curves are stacked on the axes)
order - an array specifying the order of legend entries
h - axis handle (optional, defaults to current axis)
Invoking the function without an argument or with order== causes the labels of the curves to be returned (or displayed, if nargout==0).
>> x = 0:.1:10;
>> plot(x,x,x,2*x,x,3*x,x,4*x); legend('x','2x','3x','4x');
The indices of the labels (i.e., 1 to 4 in the above e.g.)can be used to specify the desired order of legend entries.
e.g. >> reorderLegend([4,2,1,3]); % legend: 'x','3x','4x','2x' (from top to bottom)
- By default, the legend entries are in the reverse order of the indices (because the curves are in a stack).
- `reorderLegend' doesn't change the order of the curves (i.e., the indices are not changed when it's called).
Damith Senaratne (2021). reorderLegend (https://www.mathworks.com/matlabcentral/fileexchange/37265-reorderlegend), MATLAB Central File Exchange. Retrieved .
Nice function. I updated line 41 to the follow, so that plots including both lines and/or bars can have their legends reordered:
hC = findobj(h,'Type','line','-or','Type','bar');
Awesomness value for this script: plus infinity.
Now I see that this can work for bar graphs, by changing 'line' to 'bar in the following code line:
hC = findobj(h,'Type','line');
Thanks! works perfectly!
Thank you for making this function! I was so frustrated trying to flip my legend entries without switching the order of the lines on the graph (since my lines overlap but need to be seen in a particular order for visibility purposes). Works perfectly right out of the box!
This does exactly what I want, but it doesnt work for errorbar plot. Please could you modify the code?
Wow, this is great! I've had quite a struggle, but this works as a charm. Thanks a lot. Note that when specifying which order your legend items should take, for instance 3 above, 1 below that and 2 down, then enter reorderLegend([2 1 3]) since it works in reverse order.
Thanks a lot for this great little script!
Thanks for writing this script! I have been looking for something to reorder legend items while leaving the line order the same.
I am having trouble with trying to deal with legends for plotyy graphs in subplots though. I'm a) not sure how to get the handle and b) not sure how to work the ordering considering that the legend contains the items from both the y plots combined. If you can help that would be great but I appreciate this is more to do with my misunderstanding of handles! Many thanks, Cat
It's great script! Thank you!
Great little script!
Found a couple of minor issues, but both with easy workarounds:
1) Reordering sometimes doesn't work for me unless I specify the relevant plot handle(s) as the second argument (not sure why this is the case but it's easy enough information to supply, so not a major issue).
2) Since the legend is recreated within the function any formating (e.g. positioning) of the original legend is lost upon reordering. A simple modification to the function to return a handle to the new legend enables new formatting to be applied as desired though, without disturbing the reordering.
Actually, I find the answer of my question. You may just modify your code as follows, then it becomes globally applicable to any chart type and even combination of different chart types:
if nargin < 2
h = get(gca,'Children');
hC = findobj(h);
Please mention that "Type" can be changed in your code into "area" and "bar", to use it for other chart types as well.
A question: can you explain how I can use this for reordering "line" and "area" types together. I have a chart with one line on top and several areas below that.
@Kim, Get the handle to the desired axes and pass it as the second argument to reorderLegend. That should work.
Pls. try this example:
>> x = 0:.01:pi;
>> subplot(2,1,1); plot(x,sin(x),x,cos(x)); legend('sin(x)','cos(x)'); % first set of axes and curves
>> subplot(2,1,2); plot(x,sin(x).^2,x,cos(x).^2); legend('sin^2(x)','cos^2(x)'); % second set of axes and curves
>> ha = findobj(h,'Type','axes','Tag',''); % get the axes (legends are also axes but with 'Tag' == 'legend')
>> % ha is a stack; first element is the handle to the axes created last
>> reorderLegend(,ha(1)) % pass order ==  with the handle to get the indices
>> reorderLegend([1,2],ha(1)) % call the function with the desired order and the same handle
Hope this helps
Now, how to do this when there are multiple axes on a chart...
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!