Normalizing Histogram but with a line instead of bars
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
Image Analyst
on 20 Jul 2012
What do you mean by normalized? How can a line be normalized? Do you mean that you want the counts normalized, instead of the line, like if you divided by the max count, or the sum of all counts or something?
Vivi
on 20 Jul 2012
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);
the cyclist
on 20 Jul 2012
0 votes
This code from the File Exchange seems to do what you want:
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!