Legend doesn't match plot
45 views (last 30 days)
Show older comments
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
Accepted Answer
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'})
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!