Add legends to specific data points

22 visualizaciones (últimos 30 días)
Megan Powell
Megan Powell el 18 de Ag. de 2021
Comentada: Megan Powell el 19 de Ag. de 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')

Respuesta aceptada

DGM
DGM el 18 de Ag. de 2021
Editada: DGM el 18 de Ag. de 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

Más respuestas (0)

Categorías

Más información sobre Errorbars en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by