Add legends to specific data points
22 views (last 30 days)
Show older comments

Hi, I want to add a legend outlining the different coloured points on the plot, could someone help please.
i=0;
for i=1:21
if (i >=1 && i<=11)
% Plotting data points
h=errorbar(x(i),y(i),err(i),'o', 'LineWidth', 2, 'MarkerFaceColor','g','Color','g');
legend(h, 'Loading')
hold on;
% Plotting line of best fit
else
h2=errorbar(x(i),y(i),err(i),'o', 'LineWidth', 2, 'MarkerFaceColor','b','Color','b');
legend(h2, 'Unloading')
hold on;
end
end
plot(x, yfit, '-r','LineWidth', 2, 'DisplayName', 'Regression Line')
0 Comments
Accepted Answer
DGM
on 18 Aug 2021
Edited: DGM
on 18 Aug 2021
Something like this:
% fake data
x = 1:21;
y = (1:21) + 0.2*randn(1,21);
yfit = x;
err = ones(1,21);
hold on;
h(1) = errorbar(x(1:11),y(1:11),err(1:11),'o', 'LineWidth', 2, 'MarkerFaceColor','g','Color','g');
h(2) = errorbar(x(12:end),y(12:end),err(12:end),'o', 'LineWidth', 2, 'MarkerFaceColor','b','Color','b');
h(3) = plot(x, yfit, '-r','LineWidth', 2);
legend(h,{'Loading','Unloading','Regression'},'location','northwest')
box on
More Answers (0)
See Also
Categories
Find more on Errorbars 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!