Plotting 3 sets of data with error bars in different colors

조회 수: 25 (최근 30일)
Nicholas Simin
Nicholas Simin 2015년 8월 13일
편집: Daniel Carvalho 2023년 3월 27일
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

채택된 답변

Star Strider
Star Strider 2015년 8월 13일
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
Josef Henthorn
Josef Henthorn 2020년 10월 28일
Is there a better way to color each error bar from an array of RGB values?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

dpb
dpb 2015년 8월 13일
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 2023년 2월 27일
편집: Daniel Carvalho 2023년 3월 27일
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!

카테고리

Help CenterFile Exchange에서 Errorbars에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by