Change error bar colour and data point colour in a loop

41 ビュー (過去 30 日間)
Megan Powell
Megan Powell 2021 年 8 月 18 日
コメント済み: Megan Powell 2021 年 8 月 18 日
Hi, I am trying to plot 21 points and attached error bars in two different colours with the first 11 points and error bars being green and the next 10 being blue. This is as they show different results, as one is when the voltage is increased and the other is for when it is decreased.
The data points themselves do the correct thing, but the errorbars change colour with every loop.
I think I need to individually change the colour of the error bars but I can't seem to find a way to do that, I have attached the output graph, please could someone help :(
% General line of best fit without it, constrained through the origin
% Regression Of Line Through Origin
B = x(:)\y(:);
yfit = x(:)*B;
figure(1)
% Least squares equation for error
err = sqrt((abs(y-yfit)).^2);
i=0;
for i=1:21
if (i >=1 && i<=11)
% Plotting data points
hold on;
errorbar(x(i),y(i),err(i),'o', 'LineWidth', 2, 'MarkerFaceColor','g');
else
errorbar(x(i),y(i), err(i),'o', 'LineWidth', 2, 'MarkerFaceColor','b')
hold on;
end
% Plotting line of best fit
plot(x, yfit, '-r','LineWidth', 2)
end
% Further code is aesthetic only

採用された回答

David Hill
David Hill 2021 年 8 月 18 日
errorbar(x(i),y(i),err(i),'o', 'LineWidth', 2, 'MarkerFaceColor','g','Color','g');

その他の回答 (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