File Exchange

## Normalized histogram

version 1.1.0.0 (1.66 KB) by
Same as histogram, but the area sum is 1.

Updated 22 Mar 2013

HISTNORM Histogram normalized
[...] = HISTNORM(...) works like HIST, but the frequency is normalized so that area sum is 1.

Bonus usage!
[...] = HISTNORM(..., 'plot') plots and returns the output arguments. Be sure 'plot' is the last argument.

Example:
data = randn(1)+randn(1)*randn (10000, 1);
[xo,no] = histnorm(data, 101, 'plot');
hold on
plot (no, normpdf(no, mean(data), std(data)), 'r');
hold off

### Cite As

Arturo Serrano (2021). Normalized histogram (https://www.mathworks.com/matlabcentral/fileexchange/22802-normalized-histogram), MATLAB Central File Exchange. Retrieved .

RAM KUMAR J

Ruben

To work with data values lower than 1 replace 'xonorm' (line 33) var by:
xonorm = xo/sum (xo);

Note: function valid to normalize data in a vector, it is NOT valid for data in matrix format with more than one distribution to be represented.
To do multi-distributions normalization it is required further adjustments.

Mbvalentin

It doesn't seem to work for values of data lower than 1. (mu < 1).

Andoni Medina

Sorry just found out the answer! many thanks!!

Andoni Medina

Very nice! How can we add the distribution fitting? (as shown on the image) thanks!

Thangarajah Akilan

?

R

it work fantastic:)how could we use histnorm to calculate normalized histogram for hsv histogram

R

R

Damien

thanks!!!

arnold

nice one, thanks!

Arturo Serrano

The *area* must sum up to 1.
Thanks for rating!

Linpeng

The columns sum up to 10 instead of 1. Any ideas?

Arturo Serrano

*Why* is not working? I don't see any problems. Maybe if you give some clues, we can help.

Bernoulli Lizard

This does not work. The height of my bins is on the order of 10^4. What is wrong?

ww

Fantastic :)

Flavio Prattico

Great!!!

Bob

disregard above comment about the mean...

Bob

Doesn't seem to work with non-zero mean data sets. I found that the code below works better. It can not handle the case where you specify your own bin widths. But it can handle a matrix of data.
e.g.: histnorm([randn(1e3,1) randn(1e3,1)+8],50)

function varargout = histnorm(varargin)

len = size(varargin{1});
n = len(2);
[xo,no] = hist (varargin{:});
binwidths = diff(no);
xonorm = xo/sum (xo .* (binwidths(1)*ones(size(xo))))*n;
bar(no,xonorm);

Anne Calder

Thank you! Works perfect!

Arturo Serrano

Hi Anne,
you can change the colors by setting properties from the matlab command window. This should does the trick:

>> set (findobj (gca, 'type', 'patch'), 'edgecolor', [0.5 0.5 0.5], 'facecolor', [0.1 0.1 0.1]);

Edgecolor and facecolor values are the RGB values for bin colors (you can use letters also as plot does).

Anne Calder

Super useful... was wondering if you can change the colors in the bins. This isn't working for me.

Gustavo

It didn't work for me. It is giving same histogram as does hist ... I mean it is not normlizing !!!

@ Author - Any idea - what i am doing wrong ?

Ian Baldwin

very useful

abdelhamid alhassi

Darcy Sabatino

Very helpful. The histnorm() function is virtually as easy as the hist() function.

##### MATLAB Release Compatibility
Created with R2008b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux