Plotting 3 sets of data with error bars in different colors
25 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nicholas Simin
am 13 Aug. 2015
Bearbeitet: Daniel Carvalho
am 27 Mär. 2023
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
0 Kommentare
Akzeptierte Antwort
Star Strider
am 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.
3 Kommentare
Josef Henthorn
am 28 Okt. 2020
Is there a better way to color each error bar from an array of RGB values?
Weitere Antworten (2)
dpb
am 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?
0 Kommentare
Daniel Carvalho
am 27 Feb. 2023
Bearbeitet: Daniel Carvalho
am 27 Mär. 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!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Errorbars finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!