Change error bar colour and data point colour in a loop
48 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
David Hill
on 18 Aug 2021
errorbar(x(i),y(i),err(i),'o', 'LineWidth', 2, 'MarkerFaceColor','g','Color','g');
More Answers (0)
See Also
Categories
Find more on Errorbars in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!