Legend doesn't match plot

47 views (last 30 days)
LM
LM on 17 Mar 2021
Commented: LM on 17 Mar 2021
Hello, my legend color doesn't match the color of my plot. I need two diffentent colors in my legend. I have already searched the other questions asked, but none of them seem to work for me.
figure
hold on
% P(1) = plot(x1,a1,'g.',x2,b1,'g.',x3,c1,'g.',x4,d1,'g.','DisplayName','a')
plot(x1,a1,'g.',x2,b1,'g.',x3,c1,'g.',x4,d1,'g.');
errH1 = errorbar(mean(x1), mean(a1), error_LG(1,Rauheitskennwert),'g+');
errH1.LineWidth = 1.2;
errH2 = errorbar(mean(x2), mean(b1), error_LS(1,Rauheitskennwert),'g+');
errH2.LineWidth = 1.2;
errH3 = errorbar(mean(x3), mean(c1), error_LgR(1,Rauheitskennwert),'g+');
errH3.LineWidth = 1.2;
errH4 = errorbar(mean(x4), mean(d1), error_RG(1,Rauheitskennwert),'g+');
errH4.LineWidth = 1.2;
xlim([0.5 4.5])
plot(x1,a_Filter,'.b',x2,b_Filter,'.b',x3,c_Filter,'.b',x4,d_Filter,'.b');
% P(2) = plot(3,a_Filter,'.b',3,b_Filter,'.b',3,c_Filter,'.b',3,d_Filter,'.b','DisplayName','b')
errH6 = errorbar(mean(x1), mean(a_Filter), std(a_Filter),'b+');
errH6.LineWidth = 1.2;
errH7 = errorbar(mean(x2), mean(b_Filter), std(b_Filter),'b+');
errH7.LineWidth = 1.2;
errH8 = errorbar(mean(x3), mean(c_Filter), std(c_Filter),'b+');
errH8.LineWidth = 1.2;
errH9 = errorbar(mean(x4), mean(d_Filter), std(d_Filter),'b+');
errH9.LineWidth = 1.2;
% legendNames = ['a','b'];
% legend(P)
legend('a','b')
  2 Comments
Çağlar  İŞLEK
Çağlar İŞLEK on 17 Mar 2021
Hi, Please share your output plot, I will look into it.
LM
LM on 17 Mar 2021
Hey, thanks for your answer. You should be able to view it below my name. It is called 'Screenshot 2021-03-17 105128.png'. But Roy Kadesh's answer has already solved my problem. Thanks a lot for your willing to help me though.

Sign in to comment.

Accepted Answer

Roy Kadesh
Roy Kadesh on 17 Mar 2021
You did not explicitly state the handles for the legend, so Matlab uses the first two, which happen to be both green, since your plot call returns 4 objects.
%these are the required edits:
h_green=plot(x1,a1,'g.',x2,b1,'g.',x3,c1,'g.',x4,d1,'g.');
h_blue=plot(x1,a_Filter,'.b',x2,b_Filter,'.b',x3,c_Filter,'.b',x4,d_Filter,'.b');
legend([h_green(1) h_blue(1)],{'a','b'})
  1 Comment
LM
LM on 17 Mar 2021
Thanks a lot for your help and explanation. That's exactly what i was doing wrong.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!