MATLAB: Assign multiple colors to text in legend
Asked Answered
W

3

9

I'm trying to color code text in a legend. (Since I'm trying to sort several plots into different categories, I can't just rely on the line colors in the legend.) I've managed to set the text color for the entire legend, but I can't manage to assign it line by line. Is this possible?

Code so far:

list={'Label 1','Label 2','Label 3'};
leg=legend(list);
set(leg,'Textcolor',[1 0 0])

sets the text color for the entire legend as red. I'd like to be able to make some red, and some black. I tried assigning the color array as an n x 3 matrix, but MATLAB doesn't like that very much. I also poked around the legend properties using get(leg), but I couldn't find anything else that seemed useful. Any suggestions?

Wade answered 3/2, 2010 at 17:50 Comment(0)
R
9

Here is the code:

legtxt=findobj(leg,'type','text');
set(legtxt(1),'color','k')

Just find out which legends correspond to which index.

Responsory answered 3/2, 2010 at 18:3 Comment(0)
S
12

While the answers by yuk and gnovice are correct, I would like to point out a little-known and yet fully-documented fact that the legend function returns additional handles that correspond to the legend components. From the documentation of the legend function:

[legend_h, object_h, plot_h, text_strings] = legend(...) returns

  • legend_h — Handle of the legend axes
  • object_h — Handles of the line, patch, and text graphics objects used in the legend
  • plot_h — Handles of the lines and other objects used in the plot
  • text_strings — Cell array of the text strings used in the legend

These handles enable you to modify the properties of the respective objects.

Showman answered 3/2, 2010 at 22:22 Comment(2)
Thanks for pointing that out! I guess I should read the documentation file more closely next time.Wade
Good catch, Yair. That'll teach me to completely read the current documentation instead of answering from memory. ;)Cystoid
R
9

Here is the code:

legtxt=findobj(leg,'type','text');
set(legtxt(1),'color','k')

Just find out which legends correspond to which index.

Responsory answered 3/2, 2010 at 18:3 Comment(0)
C
8

To change the legend text colors individually, you have to first get the handles to the text objects, which are children of the legend object. Then you can change their text colors separately. Here's an example of how you can do it:

plot(1:10, rand(1, 10), 'r');       % Plot a random line in red
hold on;
plot(1:10, rand(1, 10), 'b');       % Plot a random line in blue
hLegend = legend('a', 'b');         % Create the legend
hKids = get(hLegend, 'Children');   % Get the legend children
hText = hKids(strcmp(get(hKids, 'Type'), 'text'));  % Select the legend children
                                                    %    of type 'text'
set(hText, {'Color'}, {'b'; 'r'});  % Set the colors

Note that the color order in the last line is blue then red, in reverse order of the way the labels are passed to the legend function. The above will give you the following plot:

enter image description here

Cystoid answered 3/2, 2010 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.