# Editing colors in the legend

1.924 views (last 30 days)
Rainer on 8 Jul 2015
Commented: Image Analyst on 29 Jan 2019
The following plots 2 data sets nicely. But the colors in the legend are wrong. Is there a way to change the colors in the legend? Thanks a lot.
dataset1=rand(10,3);
dataset2=rand(10,4)*100;
hold on
plot(dataset1,'color','b')
plot(dataset2,'color','r')
legend('DATASET1', ' DATASET2')

Thorsten on 9 Jul 2015
Edited: Walter Roberson on 13 Oct 2015
You can create each plot with a handle hi, of which you store only the first entry in h. Now you can use this handle h as the first argument of your legend function. This is basically Mike Garrity's solution with a for-loop to make it more suitable for larger number of data sets:
col = rand(4,3);
for i = 1:4, X{i} = rand(10-i, 10)*2^i; end
clear h
for i = 1:4
hi = plot(X{i}, 'Color', col(i,:));
h(i) = hi(1);
if i == 1, hold on, end,
end
legend(h, {'Dataset 1' 'Dataset 2' 'Dataset 3' 'Dataset 4'})

Rainer on 9 Jul 2015
Yes, this is a great solution. Thanks.
RG on 29 Jan 2019
I'm trying to do the same on 2018b, but I still have some trouble, the legend line colors are different from the figure line colors. I looked all over the internet, hope somebody have a suggestion to solve it. Thanks in advance. Here is the code:
Color = distinguishable_colors(numel(donuts));
for n = 1 : numel(donuts)
hi = plot(i,data.donutsnumbers(:,n),'-o','color',Color(n,:));
h(i) = hi(1);
if i == 1, hold on, end,
end
legend(h,strcat(num2str(donutsShelves),'N'))
legend boxoff
Image Analyst on 29 Jan 2019
We can't run this snippet. You didn't specify distinguishable_colors, donuts, donutsShelves, and data.
And you didn't include a screenshot to help us visualize your problem.
I have absolutely NO idea what your problem is, much less how to solve it.

### More Answers (2)

Image Analyst on 8 Jul 2015
You're plotting 20 rows of data, yet you're only passing in 2 legend strings. So it takes the colors of those two from the first two that you plotted, which were in blue color. To fix, it's probably easiest to just plot each row one at a time.
dataset1=rand(10,3);
dataset2=rand(10,4)*10;
for row = 1 : size(dataset1, 1)
plot(dataset1(row, :),'bd-')
hold on
plot(dataset2(row, :), 'ro-')
end
legend('DATASET1', ' DATASET2')

Show 3 older comments
Rainer on 9 Jul 2015
This is what I hope to get. Notice there are 2 datasets with differing sizes. I plotted this in a round about way (see code below). Now if I had 10 datasets each with differing sizes, putting them into a loop gets kind of clumsy. Doable of course. Wished there was a way to simply create a custom legend.
data=rand(10,10);
data2=rand(20,5)*10;
hold on
h1=plot(data(:,1),'color','b')
h2=plot(data2(:,1),'color','r')
legend([h1 h2],'Dataset 1','Dataset 2')
h1=plot(data(:,2:6),'color','b')
h2=plot(data2(:,2:5),'color','r')
Image Analyst on 9 Jul 2015
Other than the position of the legend, isn't that exactly what my first answer shows? If you want the legend in the northwest corner, change the legend call to this:
legend('DATASET1', ' DATASET2', 'Location', 'Northwest');
Loops are not clumsy. In fact, it lets you do exactly what you want to do in an intuitive straightforward manner. Alternatively, using findobj(), do you really want to get into calling findobj() and then figuring out what handles are lines, text, and borders and then changing them with calls to set()? Talk about clumsy, it's also arcane, confusing, and difficult.
A third option is to just create a legend yourself with these calls
text(x1,y1, 'DATASET1', 'Color', 'r', 'FontSize', 14);
text(x1,y2, 'DATASET2', 'Color', 'r', 'FontSize', 14);
line(....... % First line
line(........... % Second line
rectangle(........ % The containing box.
Mike Garrity on 9 Jul 2015
Another simple variant of this would be to give legend the handles of the objects you want it to use:
dataset1=rand(10,3);
dataset2=rand(10,4)*100;
hold on
h1 = plot(dataset1,'color','b')
h2 = plot(dataset2,'color','r')
legend([h1(1), h2(1)], 'DATASET1', ' DATASET2')
This is telling legend that you want one entry for the first item from the first call to plot (i.e. h1(1)) and another entry for the first item from the second call to plot (i.e. h2(1)). Does that make sense?

Joseph Cheng on 8 Jul 2015
well... you can adjust the handles of the legend through something like this
dataset1=rand(10,3);
dataset2=rand(10,4)*100;
hold on
plot(dataset1,'color','b')
plot(dataset2,'color','r')
hleg = legend('DATASET1', 'DATASET2');
chleg = get(hleg,'children')
set(chleg(1),'color','r')
set(chleg(2),'color','g')
set(chleg(3),'color','y')
set(chleg(4),'color','r')
set(chleg(5),'color','m')
set(chleg(6),'color','c')
I chose to change all values to different colors so you can see how the children are formed. i used to know why there are 6 children but i can't seem to recall at the moment. However from trying you can see that the symbol for each entry is chleg(2) and chleg(5). Maybe its somewhere in my previous answers submissions. however just make sure you correctly change accordingly.

Image Analyst on 8 Jul 2015
Note: does not work for R2015a, and I imagine it doesn't work with R2104b either. Not sure if it works for earlier versions.
Joseph Cheng on 8 Jul 2015
I really should upgrade again, but works in R2014a.
Ian Hunter on 25 Jul 2018
does not work in R2016a