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
the cyclist 2013년 8월 29일

9 개 추천

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

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
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
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
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.
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
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
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
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
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
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
Leidy Castro Meneses 2017년 3월 10일
편집: Leidy Castro Meneses 2017년 3월 10일

2 개 추천

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
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에 대해 자세히 알아보기

질문:

2013년 8월 29일

편집:

2017년 7월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by