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:
legend

Subject: legend

From: Elnaz

Date: 30 Jan, 2013 20:41:08

Message: 1 of 6

Hi all,

How can I make the legend to appear in different colors than those colors that I've plotted in?
For example if I use legend('string1','string2'); then string 1 and string 2 will appear in the same colors to their corresponding curves. However, I want to avoid this and get my legend in different colors. How can I do this?

Thanks,
Elnaz

Subject: legend

From: dpb

Date: 30 Jan, 2013 20:51:35

Message: 2 of 6

On 1/30/2013 2:41 PM, Elnaz wrote:
> Hi all,
>
> How can I make the legend to appear in different colors than those
> colors that I've plotted in?
> For example if I use legend('string1','string2'); then string 1 and
> string 2 will appear in the same colors to their corresponding curves.
> However, I want to avoid this and get my legend in different colors. How
> can I do this?

Return the optional handles and set the text object 'color' property as
desired. The legend is just an axes object similar to a plot...

doc legend

--

Subject: legend

From: dpb

Date: 30 Jan, 2013 22:20:56

Message: 3 of 6

On 1/30/2013 2:51 PM, dpb wrote:
> On 1/30/2013 2:41 PM, Elnaz wrote:
>> Hi all,
>>
>> How can I make the legend to appear in different colors than those
>> colors that I've plotted in?
>> For example if I use legend('string1','string2'); then string 1 and
>> string 2 will appear in the same colors to their corresponding curves.
>> However, I want to avoid this and get my legend in different colors. How
>> can I do this?
>
> Return the optional handles and set the text object 'color' property as
> desired. The legend is just an axes object similar to a plot...
>
> doc legend

Sorry, forgot that for a patch object the property you want is
'facecolor', not 'color'...

And, in lastest release I see the handles aren't quite so direct as I
recalled, either...except for the text, hb are hggroup so have yet
another level of indirection (afaict, anyways)...

[ha,hb]=legend(....);
% find the groups that contain patches as children...
hg=hb(ismember(get(hb,'type'),'hggroup'));
% now get the handles of the patches...
hp=get(hb,'children');
hp=set(hp,'facecolor',DESIRED_COLOR_SPEC);

Your DESIRED_COLOR_SPEC can be one of the pre-named colors 'r', 'g',
'b', ..., etc., or an RGB triplet, etc. See the doc on patch color to
see all the possibilities; they're myriad.

Above would set all, of course unless the color spec is an array the
length of the number of handles in hp.

There _may_ be a more direct route but I'm not aware of any higher-level
functions to manipulate the legend properties more conveniently. I
couldn't get the figure editor here to let me get at anything below the
legend axis properties, either...not sure if that is a bug or not.

--

Subject: legend

From: Elnaz

Date: 30 Jan, 2013 22:28:08

Message: 4 of 6

> Return the optional handles and set the text object 'color' property as
> desired. The legend is just an axes object similar to a plot...
>
> doc legend
>
> --
Could you specify what you mean by returning the optional handles?

Thanks,
Elnaz

Subject: legend

From: dpb

Date: 30 Jan, 2013 23:15:15

Message: 5 of 6

On 1/30/2013 4:28 PM, Elnaz wrote:
>> Return the optional handles and set the text object 'color' property
>> as desired. The legend is just an axes object similar to a plot...
>>
>> doc legend
...

> Could you specify what you mean by returning the optional handles?
...

See the doc for legend as suggested--the optional return values are the
various handles of the objects.

Also, see my earlier response for an example.

NB re: the comment in other response about simpler way--legend() doc
indicates one can use property,value pairs in the call but
experimentation show those are only properties for the axis, not passed
down to the subobjects so one does have to get to them indirectly it seems.

Also the documentation is somewhat misleading as to the content of the
object_h vector in that is states
object_h — Handles of the line, patch, and text graphics objects used
in the legend but trial here shows that only the 'text' handles are
specific, the remainder are smooshed together has a hggroup--a
handle-graphics group which is a structure.

--

Subject: legend

From: dpb

Date: 30 Jan, 2013 23:23:02

Message: 6 of 6

On 1/30/2013 5:15 PM, dpb wrote:
...

> Also the documentation is somewhat misleading as to the content of the
> object_h vector in that is states
> object_h — Handles of the line, patch, and text graphics objects used
> in the legend but trial here shows that only the 'text' handles are
> specific, the remainder are smooshed together has a hggroup--a
> handle-graphics group which is a structure.

Woops....ok, that's true for some cases but not for all. I had been
playing w/ a bar() plot for another poster so I just added the legend()
to it and fiddled there.

W/ a plot() where the legend is text and line (not a patch as is drawn
w/ a bar graph), then the types returned are the text, lines, etc., as
described so you can simplify it just a little from what I showed in
that you can simply separate out the handles that correspond to
'type'==line from the hb array rather than having to query the children
of a hggraph object.

--

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