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

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
legend Jerry 31 Oct, 2008 23:35:04
plot Jerry 31 Oct, 2008 23:35:04
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com