Colour mismatch between plot and legend

I don't know why the legend and plots don't match. I get the legend colours as defined below but the plot colours are all blue.
myh = line([xCenter, xCenter], [yCenter - yRadius(k2), yCenter + yRadius(k2)], ...
'LineWidth', 1, 'Color', [1,0,0]);
set(get(get(myh,'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
myh = line([xCenter - xRadius(k2), xCenter + xRadius(k2)], [yCenter, yCenter], ...
'LineWidth', 1, 'Color', [1,0,0]);
set(get(get(myh,'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
end

 采纳的回答

dpb
dpb 2014-6-24
See if this thread helps--
Particularly, note the cyclist's answer...

5 个评论

Thanks. But I'm thinking how to implement this with my coding or how can I relate it. I'm confused.
Use the handles of the objects you want legend to correlate with...
I can't run your code or see what your image is specifically to know precisely what you want that's not right/consistent, but the point was making is that need to save the handles of the lines you're wanting the legend to correlate with and then use them as the handles array in the call to legend
What it appears to be from your comment Inside the for loop, I have the given the plot command as
h = plot(x, y, 'LineWidth', 1)
means you'll have overwritten the handle variable h each pass instead of keeping them all.
Why aren't you either
a) building an array x , y and calling plot only once with the array in which case the colors will cycle automagically, or
b) set the line color in the loop when plot the line in which case I'd think legend would keep track ok on it's own as well,
plot(x,y,clr(i),'linewidth',1)
where
clr={'g';'b';'m';'c';'k'};
Must not have been the identical code; as the error says when that code ran you incremented the index into the colors array before addressing it so ran off the end.
Why not just write
figure
for k1 = 1:5
plot(x, y, colors(k1), 'LineWidth', 1);
if k1==1, hold on, end
end
? What's the point of all the extra obfuscation?
Well, thanks for your reply, I'll try it out.

请先登录,再进行评论。

更多回答(0 个)

类别

Community Treasure Hunt

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

Start Hunting!

Translated by