Normalizing Histogram but with a line instead of bars
4 views (last 30 days)
Show older comments
Hi,
I have a 20,000*1 data set called z and I am using bins (spacing) of w=-10:0.01:10.
So I can do a regular histogram plot:
hist(z,w)
But I would like to plot it as a line so I use:
[n1,wout1]=hist(z,w);
plot(wout1,n1)
But this line isn't normalised, can someone tell how to normalise my data? (Notice that the data is necessarily from randn)
Thanks!
3 Comments
Answers (2)
Image Analyst
on 20 Jul 2012
How about:
[n1,wout1]=hist(z,w);
% Normalize so that the sum of the points = 1.
normalizedCounts = n1 / sum(n1);
plot(wout1, normalizedCounts);
But that only normalizes vertically. The area under the curve is the sum of the y values times the spacing in the x direction (the bin width). To get an area of 1 (which I don't know why you'd want and I don't see any use for), you'd have to divide by the total area.
% Get total area under the curve so far.
deltaX = wout1(2) - wout1(1);
totalArea = sum(n1) * deltaX;
% Normalize so that the area under the curve = 1.
normalizedCounts = n1 / totalArea;
plot(wout1, normalizedCounts);
0 Comments
See Also
Categories
Find more on Histograms 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!