Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
reorder legend items

Subject: reorder legend items

From: Jerry

Date: 1 Nov, 2008 03:34:02

Message: 1 of 3

figure;
plot(0:10,0:10,'rx-',0:10,10:-1:0,'go--');
legend('cross','circle');

the order of the legend items depends on the oder of plotting, i.e., in the example above, it will be "rx- cross" on the top. Is there any way to switch the order without re-plot? switching the string is not hard, but I didn't figure out how to switch the whole thing, e.g., make "go-- circle" on the top. This is useful, for example, after you create a figure with a legend of many items, then for some reason, you want to change the order of the legend. (the order of plotting may be ad hoc, but you want your legend follow some rule).

Thanks.

Subject: reorder legend items

From: Husam Aldahiyat

Date: 1 Nov, 2008 03:55:04

Message: 2 of 3

Sure you can change that by giving each graphic object a handle then using the Legend form
                    legend(h,'string1','string2',...)

for example

h1=plot(a,b)
h2=findojb('color','g')

legend([h2, h1],'green plot','something')

Subject: reorder legend items

From: Jerry

Date: 1 Nov, 2008 19:25:04

Message: 3 of 3

it's easy to get the string in the legend box. however, it's difficult to get their corresponding handles of the graphy objects they represent without specifying some particular properties of the graph object in findobj. I'm trying to write a function like this

function reorderlegend(newidx,hax)

where hax is the handle of the axes.
if there are five graph objects in the axes, by newidx=[2 1 4 3 5], we'll be able to switch items 1 and 2, and switch items 3 and 4 in the legend box. if we can get the handles of the graph objects (hgraph) that the legend strings represent, we can use
legend(hgraph(newidx),str(newidx)) to achieve our goal.

"Husam Aldahiyat" <numandina@gmail.com> wrote in message <gegjuo$qko$1@fred.mathworks.com>...
> Sure you can change that by giving each graphic object a handle then using the Legend form
> legend(h,'string1','string2',...)
>
> for example
>
> h1=plot(a,b)
> h2=findojb('color','g')
>
> legend([h2, h1],'green plot','something')
>

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us