Bar with errorbars on the same figure
显示 更早的评论
Hi
I'm trying to plot bar with errorbars on the same figure. I tryed to use barweb (<http://www.mathworks.com/matlabcentral/fileexchange/10803-barweb-bargraph-with-error-bars>) but it doesn't seem to work. Is there an inbuilt function in Matlab?
The data I'm working with is similar to this:
mean_velocity = [0.2574, 0.1225, 0.1787]; % mean velocity
std_velocity = [0.3314, 0.2278, 0.2836]; % standard deviation of velocity
回答(2 个)
the cyclist
2013-8-29
There is not a built-in for this, but you can superpose an errorbar() chart with a bar chart:
mean_velocity = [0.2574, 0.1225, 0.1787]; % mean velocity
std_velocity = [0.3314, 0.2278, 0.2836]; % standard deviation of velocity
figure
hold on
bar(1:3,mean_velocity)
errorbar(1:3,mean_velocity,std_velocity,'.')
I feel that this may not be exactly what you want, but it should give you an idea of what is possible
10 个评论
Amit Kenny
2013-9-12
this function work only when you have one bar per category. how do you solve the problem with a little beat more complex graph, like:
Y=[1,2;3,4];Errors=[0.2,0.5;0.4,0.1];X=[1,2];
now if you try
bar(X,Y); hold on; Errorbar(X,Y,Errors,'.')
you will get an error massage
??? Error using ==> errorbar at 76
X, Y and error bars must all be the same length
I will acknowledge a solution
the cyclist
2013-9-12
编辑:the cyclist
2013-9-12
This is not very easy, because the individual bars are offset from the X data location. However, you can get at those data. Here is an example:
mean_velocity = [5 6 7; 8 9 10]; % mean velocity
std_velocity = randn(2,3); % standard deviation of velocity
figure
hold on
hb = bar(1:3,mean_velocity');
% For each set of bars, find the centers of the bars, and write error bars
for ib = 1:numel(hb)
% Find the centers of the bars
xData = get(get(hb(ib),'Children'),'XData')
barCenters = mean(unique(xData,'rows'))
errorbar(barCenters,mean_velocity(ib,:),std_velocity(ib,:),'k.')
end
Gerdian Budiar
2015-4-15
Hi the ciclist. I used your code with a previous Matlab version and it worked perfectly fine. Now I have R2015a and I get this error:
Error using errorbar (line 37)
X, Y, and error bars all must be the same length.
Error in boronic_acid_modif_bars (line 20)
errorbar(barCenters,rejection(ib,:),std_rejection(ib,:),'k.');
Do you have idea why and how to fix it?
the cyclist
2015-4-16
I don't know how to make it work in the new version (where graphics handles are objects). I tried to figure it out, but I couldn't. I have posted a new question asking for this here.
Allison
2015-6-3
Also posted this answer in the cyclist's separate thread, but thought I'd share it here too in case anyone is looking. A slight tweak to the original code will allow it to run properly in r2014b+:
%
mean_velocity = [5 6 7; 8 9 10]; % mean velocity
std_velocity = randn(2,3); % standard deviation of velocity
figure
hold on
hb = bar(1:3,mean_velocity');
% For each set of bars, find the centers of the bars, and write error bars
pause(0.1); %pause allows the figure to be created
for ib = 1:numel(hb)
%XData property is the tick labels/group centers; XOffset is the offset
%of each distinct group
xData = hb(ib).XData+hb(ib).XOffset;
errorbar(xData,mean_velocity(ib,:),std_velocity(ib,:),'k.')
end
Still working out why the pause line is required, but it won't work properly without it. You can probably decrease the time to less than 0.1 safely- I think the engine just requires some time to register the figure handle.
Abbas Bandukwala
2015-11-11
编辑:Abbas Bandukwala
2015-11-11
Allison, Thank you for your post. I want to have multiple separate figures with bar graphs and error bars. When I use your code, it works for the first bar graph, but for the second and third functions it creates figures with just the error bars and bar plots. It also removes my first figure complete. I have used the hold on and hold off functions, and have figure() for each function. I do not understand why it works for only one of the functions. How do you stop this?
David J. Mack
2016-3-14
Very nice solution! But how did you figure out the XOffset property? Since it is not visible.... Greetings, David
Duijnhouwer
2016-6-3
@Allison
"pause" is required but not because it gives matlab some time to draw, but because, internally, "pause" calls "drawnow". just drawnow should work too
chaitra
2017-3-14
figure
hold on
bar(1:9,mean_velocity1)
errorbar(1:9,mean_velocity1,std_velocity1,'.')
figure
hold on
group = [mean_velocity1 mean_velocity2];
bar(1:9,mean_velocity2)
errorbar(1:9,mean_velocity2,std_velocity2,'.')
figure
hold on
gives me separate graphs. How shall I compare them in 1 plot with different colors?
Guillem Heinrich
2017-7-20
编辑:Guillem Heinrich
2017-7-20
@chaitra You are creating a new figure for the second plot --> comment the fifth line of your code.
Leidy Castro Meneses
2017-3-10
编辑:Leidy Castro Meneses
2017-3-10
Hi Based on the previous answer, I figured this script out:
young= [458.05,509.63]; %values are for young and old respectively
young2= [458.05,509.63,200,340];
old= [200,340];
group = [young;old];
SEM=[12,12,56,45]; % values for error bars
figure
hold on
bar(1:2,group)
errorbar([0.86,1.14,1.86,2.14],young2,SEM,'.') %errorbar(x,y,err)
1 个评论
chaitra
2017-3-14
How to change colors ?([0.86,1.14,1.86,2.14], what is the use of this?
类别
在 帮助中心 和 File Exchange 中查找有关 Errorbars 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!