Plotting 3 sets of data with error bars in different colors
25 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
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
2020 年 10 月 28 日
Is there a better way to color each error bar from an array of RGB values?
その他の回答 (2 件)
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?
0 件のコメント
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!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Errorbars についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!