Editing colors in the legend

1.428 visualizaciones (últimos 30 días)
Rainer
Rainer el 8 de Jul. de 2015
Editada: Joseph Cheng el 1 de Jun. de 2021
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')

Respuesta aceptada

Thorsten
Thorsten el 9 de Jul. de 2015
Editada: Walter Roberson el 13 de Oct. de 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'})
  3 comentarios
RG
RG el 29 de En. de 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
Image Analyst el 29 de En. de 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.

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 8 de Jul. de 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')
  6 comentarios
Image Analyst
Image Analyst el 9 de Jul. de 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
Mike Garrity el 9 de Jul. de 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?

Iniciar sesión para comentar.


Joseph Cheng
Joseph Cheng el 8 de Jul. de 2015
Editada: Joseph Cheng el 1 de Jun. de 2021
********************************************
not working past version 2014a
********************************************
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.
  8 comentarios
Image Analyst
Image Analyst el 1 de Jun. de 2021
@Konark Kelaiya, yes, several people have said this code from @Joseph Cheng doesn't work. The graphics changed quite a bit after R2014a like Joseph was using. However, no one said that about my solution. Did you try mine? Perhaps you should start your own question with your own data attached in a separate thread.
Joseph Cheng
Joseph Cheng el 1 de Jun. de 2021
definately, this appears to be more obfuscated in later versions (after 2014a). Even @Image Analyst your solution would be the better correct method which i'm surprised people still wouldn't have gone for especially read back through the posts it is the corrected runnable Answers. I originally put my 2cents in as for some reson i had to do my (2014a) method that for some reason or another that i can't remember wouldn't play nice with some proper foresight in programming the legends.
maybe i should just delete this input (circa 2015) or provide another update in another format of all the above current valid (as of 2021) solutions.
figure(1),clf,hold on;
clors = ['brgmk'];
mrks = [':.-*^'];
for ind = 1:5
hplt(ind) = plot(randi(10,10,1),[clors(ind) mrks(ind)]);
ltxt{ind} = ['legend text' num2str(ind)];
end
legend(hplt(1:2:end),ltxt(1:2:end))

Iniciar sesión para comentar.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by