Add legends to specific data points

22 vues (au cours des 30 derniers jours)
Megan Powell
Megan Powell le 18 Août 2021
Commenté : Megan Powell le 19 Août 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')

Réponse acceptée

DGM
DGM le 18 Août 2021
Modifié(e) : DGM le 18 Août 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

Plus de réponses (0)

Catégories

En savoir plus sur Errorbars dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by