1
votes

I'm new to MATLAB, and I have a set of data that I need to plot on a histogram. Furthermore, based on the estimated standard deviation and mean of my sample, I need to overlay a Gaussian with these parameters onto the figure. Finally, I would like to plot vertical lines that indicate the mean and +/- 1 standard deviation. What is the best way to do so? I have tried using the histfit function, but then on the figure when trying to add the vertical bars for the standard deviation (Tools->Data Statistics), the "std" value is not the same as that calculated by std(data) (I would like to know why). Does anybody know of a way to do all of this?

Thanks!

1
The standard deviation of a sample indicates the deviation of the data from its mean; you have basically two main options to present data from gaussian distributions: 1) you plot an histogram, and the sample's deviation can be viewed as a distance in the xx axis, so you don't need vertical error bars, or 2) you can plot the average of the distribution as a dot and according to its standard deviation add a vertical error bar... - sissi_luaty
can you be more clear in what do you want/need to do, in order to get some help? - sissi_luaty
@siluaty I would like to plot a histogram. I don't mean that I want to plot error bars; rather I would like to just have a vertical line indicating on the x-axis the position of the mean and one standard deviation away from the mean. - Andrew Martin

1 Answers

2
votes

I use the line function:

% This outputs a histogram with lines at the mean, 
%    and +/- 1 standard deviation levels. 
%    It does not normalize the data 
%    nor apply a normal Gaussian bell curve over it.

figure(1)
[Y,X] = hist(DATA,n);
sigma = std(DATA);
xbar = mean(DATA);
bar(X,Y)
y = ylim;
line([1 1]*(xbar-sigma),y)
line([1 1]*xbar,y)
line([1 1]*(xbar+sigma),y)

I've never used the histfit function, but my suspicion is that it is doing a least squares type fit of a normal distribution to your histogram. This will result in a different standard deviation than what you will compute with std unless your data set has an infinite number of data points and it is truly normally distributed.