Look at the _'Normalization'_ property. Answer from Sean de Wolski on mathworks.com
🌐
MathWorks
mathworks.com › matlab › graphics › 2-d and 3-d plots › data distribution plots
Histogram - Histogram plot - MATLAB
... Create histograms by passing ... name automatically. You can create histograms with percentages on the vertical axis by setting the Normalization name-value argument to 'percentage'....
Discussions

Normalization pdf histogram and cdf
Hi, I am using this code in MATLAB: histogram(my data,'Normalization','pdf'); after plotting the pdf histogram, the y axis is in a range between 0 to 100. But I need to have the y axis in a rang... More on mathworks.com
🌐 mathworks.com
2
0
May 2, 2020
How to normalize a histogram in MATLAB? - Stack Overflow
How to normalize a histogram such that the area under the probability density function is equal to 1? More on stackoverflow.com
🌐 stackoverflow.com
Normalizing a histogram
Normalizing a histogram. Learn more about histogram More on mathworks.com
🌐 mathworks.com
5
0
March 31, 2012
How do you normalize a histogram from the hist() function of MATLAB? - Stack Overflow
I wish to normalize my histogram, but for some reason I get some error in my code. More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 7
123

My answer to this is the same as in an answer to your earlier question. For a probability density function, the integral over the entire space is 1. Dividing by the sum will not give you the correct density. To get the right density, you must divide by the area. To illustrate my point, try the following example.

[f, x] = hist(randn(10000, 1), 50); % Create histogram from a normal distribution.
g = 1 / sqrt(2 * pi) * exp(-0.5 * x .^ 2); % pdf of the normal distribution

% METHOD 1: DIVIDE BY SUM
figure(1)
bar(x, f / sum(f)); hold on
plot(x, g, 'r'); hold off

% METHOD 2: DIVIDE BY AREA
figure(2)
bar(x, f / trapz(x, f)); hold on
plot(x, g, 'r'); hold off

You can see for yourself which method agrees with the correct answer (red curve).

Another method (more straightforward than method 2) to normalize the histogram is to divide by sum(f * dx) which expresses the integral of the probability density function, i.e.

% METHOD 3: DIVIDE BY AREA USING sum()
figure(3)
dx = diff(x(1:2))
bar(x, f / sum(f * dx)); hold on
plot(x, g, 'r'); hold off
2 of 7
24

Since 2014b, Matlab has these normalization routines embedded natively in the histogram function (see the help file for the 6 routines this function offers). Here is an example using the PDF normalization (the sum of all the bins is 1).

data = 2*randn(5000,1) + 5;             % generate normal random (m=5, std=2)
h = histogram(data,'Normalization','pdf')   % PDF normalization

The corresponding PDF is

Nbins = h.NumBins;
edges = h.BinEdges; 
x = zeros(1,Nbins);
for counter=1:Nbins
    midPointShift = abs(edges(counter)-edges(counter+1))/2;
    x(counter) = edges(counter)+midPointShift;
end

mu = mean(data);
sigma = std(data);

f = exp(-(x-mu).^2./(2*sigma^2))./(sigma*sqrt(2*pi));

The two together gives

hold on;
plot(x,f,'LineWidth',1.5)

An improvement that might very well be due to the success of the actual question and accepted answer!


EDIT - The use of hist and histc is not recommended now, and histogram should be used instead. Beware that none of the 6 ways of creating bins with this new function will produce the bins hist and histc produce. There is a Matlab script to update former code to fit the way histogram is called (bin edges instead of bin centers - link). By doing so, one can compare the pdf normalization methods of @abcd (trapz and sum) and Matlab (pdf).

The 3 pdf normalization method give nearly identical results (within the range of eps).

TEST:

A = randn(10000,1);
centers = -6:0.5:6;
d = diff(centers)/2;
edges = [centers(1)-d(1), centers(1:end-1)+d, centers(end)+d(end)];
edges(2:end) = edges(2:end)+eps(edges(2:end));

figure;
subplot(2,2,1);
hist(A,centers);
title('HIST not normalized');

subplot(2,2,2);
h = histogram(A,edges);
title('HISTOGRAM not normalized');

subplot(2,2,3)
[counts, centers] = hist(A,centers); %get the count with hist
bar(centers,counts/trapz(centers,counts))
title('HIST with PDF normalization');


subplot(2,2,4)
h = histogram(A,edges,'Normalization','pdf')
title('HISTOGRAM with PDF normalization');

dx = diff(centers(1:2))
normalization_difference_trapz = abs(counts/trapz(centers,counts) - h.Values);
normalization_difference_sum = abs(counts/sum(counts*dx) - h.Values);

max(normalization_difference_trapz)
max(normalization_difference_sum)

The maximum difference between the new PDF normalization and the former one is 5.5511e-17.

🌐
MathWorks
mathworks.com › matlabcentral › answers › 34099-normalizing-a-histogram
Normalizing a histogram - MATLAB Answers - MATLAB Central
March 31, 2012 - https://www.mathworks.com/matlabcentral/answers/34099-normalizing-a-histogram#answer_42748 · Cancel Copy to Clipboard · 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.
🌐
GeeksforGeeks
geeksforgeeks.org › software engineering › how-to-normalize-a-histogram-in-matlab
How to Normalize a Histogram in MATLAB? - GeeksforGeeks
December 8, 2021 - Maximum and minimum intensity is noted from the histogram. The image data type is changed from uint8 to double, to facilitate the calculation steps. Apply the formula of normalization.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 72466206 › how-do-you-normalize-a-histogram-from-the-hist-function-of-matlab
How do you normalize a histogram from the hist() function of MATLAB? - Stack Overflow
There are normalisation options as name-value pairs when creating the histogram. histogram(x,bin,'Normalization','pdf'); or histogram(x,bin,'Normalization','probability');, for example, may be what you are looking for.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1562306-how-can-i-normalize-the-histogram
how can ı normalize the histogram? - MATLAB Answers - MATLAB Central
October 12, 2021 - Here is an example code snippet that demonstrates how to normalize a histogram and overlay a fitted distribution: ... Sign in to comment. Sign in to answer this question. Find more on Data Distribution Plots in Help Center and File Exchange ... Find the treasures in MATLAB Central and discover how the community can help you!
🌐
MathWorks
mathworks.com › matlabcentral › answers › 866185-how-can-i-normalize-a-histogram-dividing-by-the-maximum-number-of-observations-in-a-bin-so-that-th
How can I normalize a histogram (dividing by the maximum number of observations in a bin, so that the histogram maximum = 1). - MATLAB Answers - MATLAB Central
June 27, 2021 - Usually histograms are bar charts and neither of those is. It looks like the black line is some kind of actual data and the red line is a log-normal, or rayleigh fit to the black data. ... https://www.mathworks.com/matlabcentral/answers/866185-how-can-i-normalize-a-histogram-dividing-by-the-maximum-number-of-observations-in-a-bin-so-that-th#comment_1607485
🌐
MathWorks
mathworks.com › matlab › graphics › 2-d and 3-d plots › data distribution plots
histcounts - Histogram bin counts - MATLAB
Example: [N,edges] = histcounts(X,'Normalization','probability') normalizes the bin counts in N, such that sum(N) is 1.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1674089-normalizing-histogram-counts-to-1
Normalizing histogram counts to 1 - MATLAB Answers - MATLAB Central
March 17, 2022 - Hi, I am trying to normalize the histogram counts in the from of 0 to 1 using the below script. However, I am not getting my counts in the range of 0 to 1. PS - I have also attached my data here...
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1639285-normalised-the-histogram-of-an-image
NORMALISED THE HISTOGRAM OF AN IMAGE. - MATLAB Answers - MATLAB Central
January 30, 2022 - I'm asking "why?". That is called histogram equalization (not "normalization") and usually is not necessary and gives an image that doesn't look good or natural. I recommend you do not use histogram equalization. It is not needed. Sign in to comment. ... https://www.mathworks.com/matlabcentral/answers/1639285-normalised-the-histogram-of-an-image#answer_891705
🌐
Plotly
plotly.com › matlab › data-distribution-plots › histogram
MATLAB histogram | Plotly Graphing Library for MATLAB® | Plotly
Since the sample size and bin width of the histograms are different, it is difficult to compare them. Normalize the histograms so that all of the bar heights add to 1, and use a uniform bin width.
Top answer
1 of 2
1
For probability, each element in the output is the number of elements in the input that fall into that bin divided by the total number of elements in the input. So if you sum the elements in the output, what you get is the total number of elements in the input that fall into any of the bins divided by the total number. That's why its row in the table in the description of the 'Normalization' name-value argument says "The sum of the bin values is less than or equal to 1." It can be less than 1 if the 'BinLimits' or 'BinEdges' that you specified exclude one or more of the points in the input from being assigned into any of the bins, for example. For pdf, each element in the output is the number of elements in the input that fall into that bin divided by the product of the width of the bin and the total number of elements in the input. If each of your bins were 1 unit wide, the 'pdf' and the 'probability' would be the same. If each of your bins were 0.1 units wide, each element in the output normalized by 'pdf' would be ten times as large as the corresponding element in the output normalized by 'probability' and if I summed the output of 'pdf' normalization I'd expect to get a result of 10. x = randn(1, 1e5); prob_BW1 = histcounts(x, 'BinWidth', 1, 'Normalization', 'probability'); pdf_BW1 = histcounts(x, 'BinWidth', 1, 'Normalization', 'pdf'); prob_BWtenth = histcounts(x, 'BinWidth', 0.1, 'Normalization', 'probability'); pdf_BWtenth = histcounts(x, 'BinWidth', 0.1, 'Normalization', 'pdf'); format longg shouldBeSame = [prob_BW1.', pdf_BW1.'] BWtenth_results = [prob_BWtenth; pdf_BWtenth; pdf_BWtenth./prob_BWtenth].' All the elements in the third column of BWtenth_results are either 10 (or close to it) or NaN (if there's no data in x that fell into that particular bin.) And as I said above, the sum of the probabilities is 1 but the sum of the PDF values is 10 because the bin width was 1/10. [sum(prob_BWtenth), sum(pdf_BWtenth)] All those calculations I did assumed that the bin width was the same for each bin. If your bins had different widths (because you selected a non-uniformly spaced set of BinEdges) then the equivalent of the third column of BWtenth_results for that set of bins would reflect the spacing for each different bin.
2 of 2
1
PDF is the probability density, not the probability. To get the probability for a given bin, you need to multiply by the bin width. Your sum of C does not take that into account. MATLAB's "probability" normalization (your B calculation) is doing that for you.
🌐
Plotly
plotly.com › matlab › histograms
Histograms in MATLAB
Since the sample size and bin width of the histograms are different, it is difficult to compare them. Normalize the histograms so that all of the bar heights add to 1, and use a uniform bin width.
🌐
Delft Stack
delftstack.com › home › howto › matlab › histogram matlab
How to Plot Histogram in MATLAB | Delft Stack
February 2, 2024 - The value 7 repeated seven times ... as you like in the histogram. We can normalize a histogram using the Normalization property inside the histogram() function....