MATLAB Answers

Change error bar colour and data point colour in a loop

조회 수: 5(최근 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개)

Community Treasure Hunt

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

Start Hunting!

Translated by