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 日

1 投票

See if this thread helps--
Particularly, note the cyclist's answer...

5 件のコメント

Priya
Priya 2014 年 6 月 24 日
Thanks. But I'm thinking how to implement this with my coding or how can I relate it. I'm confused.
dpb
dpb 2014 年 6 月 24 日
Use the handles of the objects you want legend to correlate with...
dpb
dpb 2014 年 6 月 24 日
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'};
dpb
dpb 2014 年 6 月 24 日
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?
Priya
Priya 2014 年 6 月 25 日
Well, thanks for your reply, I'll try it out.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2014 年 6 月 24 日

編集済み:

2014 年 8 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by