Add legends to specific data points

22 ビュー (過去 30 日間)
Megan Powell
Megan Powell 2021 年 8 月 18 日
コメント済み: Megan Powell 2021 年 8 月 19 日
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')

採用された回答

DGM
DGM 2021 年 8 月 18 日
編集済み: DGM 2021 年 8 月 18 日
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
  1 件のコメント
Megan Powell
Megan Powell 2021 年 8 月 19 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeErrorbars についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by