MATLAB: Normalizing a histogram

histogram

Hello,
I've plotted a histogram of some data. Here it is http://dl.dropbox.com/u/54057365/All/departure%20time.JPG
How can remove the gaps between the bars? Should I be using a histogram? But how can you normalize the measurements on the y axis in a histogram?
Many thanks
DepartureTimes = load('Departure Times (hr).txt')
h = hist(DepartureTimes,24);
h = h/sum(h);
bar(h, 'DisplayName', 'Depature Times');
legend('show');
xlim([5 25])

Best Answer

Hi John, if you type "help hist", you'll find information about specifying the bar centers. This implicitly controls the width of the bins that the bars cover.
If you want to change the gap between the bars, see "help hist" for information about returning the bar heights instead of plotting them, and "help bar" for information about drawing bars and controlling the space between them.
For example:
X = randn(1e3,1);
N = hist(X,22);
bar(N,1);
As far as whether the histogram is appropriate or how to "normalize" it. Can you be more specific? People generally plot a histogram in two ways:
1.) the raw frequency or count histogram 2.) a probability histogram (as you have almost done), so that they can overlay a PDF for comparison.
Here's an example of that (requires Statistics Toolbox):
Data = randn(1000,1); %just making up some junk data
binWidth = 0.7; %This is the bin width
binCtrs = -3:0.7:3; %Bin centers, depends on your data
n=length(Data);
counts = hist(Data,binCtrs);
prob = counts / (n * binWidth);
H = bar(binCtrs,prob,'hist');
set(H,'facecolor',[0.5 0.5 0.5]);
% get the N(0,1) pdf on a finer grid
hold on;
x = -3:.1:3;
y = normpdf(x,0,1); %requires Statistics toolbox
plot(x,y,'k','linewidth',2);
Related Question