Legend has different color lines from plot lines
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Kelly Kyriakou
el 9 de Oct. de 2015
Editada: Kelly Kyriakou
el 10 de Oct. de 2015
I have the following code to plot some data but the line colors are different from legend's line colors. How I could corrrect it?
for allSS=1:size(scnames,1) ;
nameofsource2open = scnames{allSS,1};
allTCON =[0.3;0.5]
allTECO =[0.2;0.77]
allTENV = [0.56; 0.888]
XallSS2plot = [allTCON(allSS)*0; allTECO(allSS)*(sqrt(3))/2; allTENV(allSS)*(-((sqrt(3))/2));allTCON(allSS)*0]
YallSS2plot = [allTCON(allSS)*1; allTECO(allSS)*(-0.5); allTENV(allSS)*(-0.5);allTCON(allSS)*1]
Xindex2plot = XallSS2plot(:)
Yindex2plot = YallSS2plot(:)
axis , hold on
for i=1:size(scnames,1)
col = rand(i,3)
nameofsource2open = scnames{allSS,1};
hh(i) = plot(XallSS2plot(:,1),YallSS2plot(:,1), 'Color', col(i,:),'LineWidth',2)
legendtext{i} = scnames;
hhh = legend(hh, 'DisplayName',legendtext{i})
linecolors{i} = col(i,:)
legendlinestyles(hhh, {},{},linecolors{i})
end
end
0 comentarios
Respuesta aceptada
Geoff Hayes
el 10 de Oct. de 2015
Kelly - typically this problem of colour mismatch between what is plotted and what appears in the legend occurs because there are more calls (either explicitly or implicitly) to plot than what you show in the legend. For example see http://www.mathworks.com/matlabcentral/answers/146668-legend-and-graph-doesn-t-have-the-same-color for details.
In your case, I could only get the code to run if I set the scnames variable to a cell array with two rows. (Should this be the case?) Then, two triangles were plotted in blue (larger one) and purple (smaller one inside the other), yet the legend showed the colours as green and blue. Looking closer at your code, you have an inner for loop that does the plotting and this loop iterates over the number of rows in scnames...just like the outer for loop. So your code is calling plot four times drawing four triangles of which only two are distinct. So four colours are used but only two are shown in the legend. You may want to review this to verify whether the above logic is correct and that you do in fact mean to draw four triangles and not just two.
1 comentario
Más respuestas (0)
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!