The legend does not show the right marker:

29 次查看(过去 30 天)
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!

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2021-4-6
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by