How to assign error bar at middle of histogram?

조회 수: 2 (최근 30일)
Ashok Gurung
Ashok Gurung 2018년 5월 16일
댓글: OCDER 2018년 6월 18일
Hello everyone,
I am trying to plot the histogram with the error bar at the middle point of histogram, and I used 'bar' syntax to get the plot, but couldn't get what I was looking for. Any help will be really appreciated.
% bin edges
bi = 0 :3: 175;%bin interval
% absolute frequencies
af1 = histc(VarName1, bi);
af2 = histc(VarName2, bi);
af3 = histc(VarName3, bi);
af4 = histc(VarName4, bi);
af5 = histc(VarName5, bi);
%VarName1, VarName2,VarName3,VarName4,VarName4 are my five distribution of data
mean=(af1+af2+af3+af4+af5)/5.0;
error=sqrt(((mean-af1).^2+(mean-af2).^2+(mean-af3).^2+(mean-af4).^2+(mean-af4).^2)/5);
%histogram(mean,be)
bar(bi,mean,'histc');
hold on
errorbar(bi,mean,error,'k','linestyle','none')
Thanks,
AG

답변 (1개)

OCDER
OCDER 2018년 5월 16일
편집: OCDER 2018년 5월 16일
Here are some suggestions. To see what each line is doing, remove the ";".
%Store your data in cells so you can use loops. Example:
VarName = cell(1, 5);
for j = 1:5
VarName{j} = randi(175, 100, 1);
end
%Label your variables with 1st-letter cap, to prevent overwriting matlab
%functions. For instance, you had "mean = (af1+af2+..." which would
%override the "mean.m" matlab function! This leads to a lot of bugs.
Bin = 0:3:175;
AF = zeros(length(Bin), length(VarName)); %Store your binned data in a matrix
for j = 1:5
AF(:, j) = histc(VarName{j}, Bin);
end
Mean = mean(AF, 2);
%error=sqrt(((mean-af1).^2+(mean-af2).^2+(mean-af3).^2+(mean-af4).^2+(mean-af4).^2)/5
%Notice you have a typo. (mean-af4).^2 comes twice.
%To prevent coding error, use following practices:
Error = sqrt(sum((Mean-AF).^2, 2)/5)
%Error = std(AF, [], 2); %Did you want standard dev?
%Plot data and store the handles as variables. Makes it easier to adjust.
Bx = bar(Bin, Mean, 'histc');
hold on
Shift = diff(Bin(1:2))/2; %Need to shift your error bar to center it
Ex = errorbar(Bin+Shift, Mean, Error, 'k', 'linestyle', 'none');
hold off
%Adjust plots via those handles. Example:
Bx.FaceColor = [1 1 1]; %Make bars white
Ex.Color = [1 0 0]; %Make error bars red
  댓글 수: 6
Ashok Gurung
Ashok Gurung 2018년 6월 18일
Hey OCDER, I know it takes a long time to reply back, apology for that. I checked the code today, and it's working now. Thanks for the help and I really appreciate that. THX AG
OCDER
OCDER 2018년 6월 18일
You're welcome!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by