Add legends to specific data points

40 Ansichten (letzte 30 Tage)
Megan Powell
Megan Powell am 18 Aug. 2021
Kommentiert: Megan Powell am 19 Aug. 2021
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')

Akzeptierte Antwort

DGM
DGM am 18 Aug. 2021
Bearbeitet: DGM am 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

Weitere Antworten (0)

Kategorien

Mehr zu Errorbars finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by