The legend does not show the right marker:

26 views (last 30 days)
Im trying to plot a graph with multiple plots on it and the legend is off:
i used :
orangeColor=[0.9290 0.6940 0.1250];
blueColor=[0 0.4470 0.7410];
scatter(XTestSet(:,2),YTestSet,[],orangeColor,'filled',"o");
scatter(XTrainSet(:,2),YTrainSet,[],blueColor,'o');
hold on;
polynum1(:,i)=PolyPredictorTest;
end
for k=1:4:9
plot(Xsorttestpoly,polynum1(:,k),"LineWidth",1.5);
end
legend('Train Data','Test Data','N=1','N=5','N=9')
hold off;
Thank you!

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 6 Apr 2021
When using legend it is always preferable to use an array of handles returned from the plotting functions. Try something like:
orangeColor=[0.9290 0.6940 0.1250];
blueColor=[0 0.4470 0.7410];
ph1 = scatter(XTestSet(:,2),YTestSet,[],orangeColor,'filled',"o");'
hold on
ph2 = scatter(XTrainSet(:,2),YTrainSet,[],blueColor,'o');
polynum1(:,i) = PolyPredictorTest;
end
for k = 1:4:9
ph3(1+(k-1)/4) = plot(Xsorttestpoly,polynum1(:,k),"LineWidth",1.5);
end
legend([ph1,ph2,ph3],'Train Data','Test Data','N=1','N=5','N=9')
hold off;
HTH

More Answers (0)

Community Treasure Hunt

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

Start Hunting!