Effacer les filtres
Effacer les filtres

Plotting 3 sets of data with error bars in different colors

26 vues (au cours des 30 derniers jours)
Nicholas Simin
Nicholas Simin le 13 Août 2015
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

Réponse acceptée

Star Strider
Star Strider le 13 Août 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 commentaires
Star Strider
Star Strider le 13 Août 2015
My pleasure!
Josef Henthorn
Josef Henthorn le 28 Oct 2020
Is there a better way to color each error bar from an array of RGB values?

Connectez-vous pour commenter.

Plus de réponses (2)

dpb
dpb le 13 Août 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 le 27 Fév 2023
Modifié(e) : Daniel Carvalho le 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!

Catégories

En savoir plus sur Errorbars dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by