Remove the legends for some lines in a plot
Show older comments
Hello,
I want to plot some data points and fit a line to the data. I have done so and everything is ok; the only issue is there are some more items in the legend box corresponding to the fitted lines.
C=['b','r','g','m'];
format short
a=1:4;
for i=1:length(a)
scatter(LT, Ra(i,:),C(i),'filled');
hold on
end
grid on
grid minor
xticks([30, 40, 50, 60, 70])
xticklabels({'30','40','50','60','70'})
for i=1:4
coeffs = polyfit(LT, Ra(i,:), 1);
% Get fitted values
fittedX = linspace(min(LT), max(LT), 200);
fittedY = polyval(coeffs, fittedX);
% Plot the fitted line
plot(fittedX, fittedY,C(i) , 'LineWidth', 2);
end
lgd=legend('25%','50%','75%','100%','FontSize',20)
lgd.Title.FontSize = 20;
The resulted figure is as follows:

Although I only have
lgd=legend('25%','50%','75%','100%','FontSize',20)
in the script, the fitted lines are unwantedly shown in the legend box with no dedicated name:

My question:
How can I get rid of those extra lines in the legend box?
Any help is highly appredicated!
Accepted Answer
More Answers (1)
dpb
on 25 Jun 2020
If you only want the scatter points in legend, set the 'Annotation' property to not show the lines...
plot(fittedX, fittedY,C(i) , 'LineWidth', 2,'Annotation','off');
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!