Plotting 3 sets of data with error bars in different colors

24 views (last 30 days)
I have three sets of data, each point has its own error, I want to plot all of them on the same figure with different sets having different colors. I don't want the curve, just the points and bars.
My attempt:
errorbar(x,y1,ey1,'.k')
hold on
errorbar(x,y2,ey2,'.k')
errorbar(x,y3,ey3,'.k')
That's what I want but with each set being in a different color eg. y1 black, y2 red, y3 blue. Thanks

Accepted Answer

Star Strider
Star Strider on 13 Aug 2015
You’re almost there! You simply have to define each error bar with the colour you want:
errorbar(x,y1,ey1,'.k')
hold on
errorbar(x,y2,ey2,'.r')
errorbar(x,y3,ey3,'.b')
hold off
See if that does what you want.

More Answers (2)

dpb
dpb on 13 Aug 2015
errorbar(x,y1,ey1,'.k')
hold on
errorbar(x,y2,ey2,'.r')
errorbar(x,y3,ey3,'.b')
Why didn't you change the color if that's what you wanted?

Daniel Carvalho
Daniel Carvalho on 27 Feb 2023
Edited: Daniel Carvalho on 27 Mar 2023
As others have stated, to change the color as you plot, with no changes to the data, you can do as they suggest:
errorbar(x,y1,ey1,'.k')
hold on
errorbar(x,y2,ey2,'.r')
errorbar(x,y3,ey3,'.b')
hold off
Alternatively, you can combine all the data into 3 matrices (instead of 7 vectors) and push all the data (x, y, and ey) at once to the errorbar function. Then you can change the appearance of each of the lines after plotting, like so:
% Concatanate, column-wise, y and ey data respectively
y = [y1, y2, y3] % assuming each y is a column vector
ey = [ey1, ey2, ey3] % assuming each ey is a column vector
% Repeat x, row-wise, for each y
x = repmat(x,[3,1]) % assuming x is a row vector
% Plot 3 lines with error bars
eBar = errorbar(x,y,ey);
% Create the RGB list
theRGB = [0 0 0;... % black
1 0 0; % red
0 1 0]; % blue
% Assign the colors to each line
for i = 1:3
eBar(i).Color = theRGB(i,:);
end
% Remove line and change marker
[eBar.LineStyle] = deal('none')
[eBar.Marker] = deal('*')
This, or something similar to this, consolidated approach is better when you have many more lines to plot together. It also shows how you can feed an RGB array to color each line individually.
Note that there is probably a way to 'deal' each of the RBG values to eBar.Color thereby replacing the loop.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!