Bar with errorbars on the same figure
Show older comments
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
Answers (2)
the cyclist
on 29 Aug 2013
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 Comments
Amit Kenny
on 12 Sep 2013
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
on 12 Sep 2013
Edited: the cyclist
on 12 Sep 2013
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
on 15 Apr 2015
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
on 16 Apr 2015
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
on 3 Jun 2015
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
on 11 Nov 2015
Edited: Abbas Bandukwala
on 11 Nov 2015
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
on 14 Mar 2016
Very nice solution! But how did you figure out the XOffset property? Since it is not visible.... Greetings, David
Duijnhouwer
on 3 Jun 2016
@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
on 14 Mar 2017
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
on 20 Jul 2017
Edited: Guillem Heinrich
on 20 Jul 2017
@chaitra You are creating a new figure for the second plot --> comment the fifth line of your code.
Leidy Castro Meneses
on 10 Mar 2017
Edited: Leidy Castro Meneses
on 10 Mar 2017
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 Comment
chaitra
on 14 Mar 2017
How to change colors ?([0.86,1.14,1.86,2.14], what is the use of this?
Categories
Find more on Errorbars in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!