# histfit

Histogram with a distribution fit

## Description

example

histfit(data) plots a histogram of values in data using the number of bins equal to the square root of the number of elements in data and fits a normal density function.

example

histfit(data,nbins) plots a histogram using nbins bins and fits a normal density function.

example

histfit(data,nbins,dist) plots a histogram with nbins bins and fits a density function from the distribution specified by dist.

example

h = histfit(___) returns a vector of handles h, where h(1) is the handle to the histogram and h(2) is the handle to the density curve. It can include any of the input arguments in previous syntaxes.

## Examples

collapse all

### Histogram with a Normal Distribution Fit

Generate a sample of size 100 from a normal distribution with mean 10 and variance 1.

rng default; % For reproducibility
r = normrnd(10,1,100,1);

Construct a histogram with a normal distribution fit.

histfit(r)

### Histogram for a Given Number of Bins

Generate a sample of size 100 from a normal distribution with mean 10 and variance 1.

rng default; % For reproducibility
r = normrnd(10,1,100,1);

Construct a histogram using six bins with a normal distribution fit.

histfit(r,6)

### Histogram with a Specified Distribution Fit

Generate a sample of size 100 from a beta distribution with parameters (3,10).

rng default;  % For reproducibility
b = betarnd(3,10,100,1);

Construct a histogram using 10 bins with a beta distribution fit.

histfit(b,10,'beta')

### Histogram with a Kernel Smoothing Function Fit

Generate a sample of size 100 from a beta distribution with parameters (3,10).

rng default;  % For reproducibility
b = betarnd(3,10,[100,1]);

Construct a histogram using 10 bins with a smoothing function fit.

histfit(b,10,'kernel')

### Handle for a Histogram with a Distribution Fit

Generate a sample of size 100 from a normal distribution with mean 10 and variance 1.

rng default % for reproducibility
r = normrnd(10,1,100,1);

Construct a histogram with a normal distribution fit.

h = histfit(r,10,'normal')
h =

2x1 graphics array:

Patch
Line

Change the bar colors of the histogram.

h(1).FaceColor = [.8 .8 1];

Change the color of the density curve.

h(2).Color = [.2 .2 .2];

## Input Arguments

collapse all

### data — Input datavector

Input data, specified as a vector.

Example: data = [1.5 2.5 4.6 1.2 3.4]

Example: data = [1.5 2.5 4.6 1.2 3.4]'

Data Types: double | single

### nbins — Number of binspositive integer | [ ]

Number of bins for the histogram, specified as a positive integer. Default value is the square root of the number of elements in data, rounded up. Use [ ] for the default number of bins when fitting a distribution.

Example: y = histfit(x,8)

Example: y = histfit(x,10,'gamma')

Example: y = histfit(x,[ ],'weibull')

Data Types: double | single

### dist — Distribution to fit'normal' (default) | string

Distribution to fit to the histogram, specified as a string. The following table shows the supported distributions.

distDescription
'beta'Beta
'birnbaumsaunders'Birnbaum-Saunders
'burr'Burr Type XII
'exponential'Exponential
'extreme value' or 'ev'Extreme value
'gamma'Gamma
'generalized extreme value' or 'gev'Generalized extreme value
'generalized pareto' or 'gp'Generalized Pareto (threshold 0)
'inversegaussian'Inverse Gaussian
'logistic'Logistic
'loglogistic'Loglogistic
'lognormal'Lognormal
'nakagami'Nakagami
'negative binomial' or 'nbin'Negative binomial
'normal'Normal
'poisson'Poisson
'rayleigh'Rayleigh
'rician'Rician
'tlocationscale't location-scale
'weibull' or 'wbl'Weibull
'kernel'Nonparametric kernel-smoothing distribution. The density is evaluated at 100 equally spaced points that cover the range of the data in data. It works best with continuously distributed samples.

Data Types: char

## Output Arguments

collapse all

### h — Handles for the plotplot handle

Handles for the plot, returned as a vector, where h(1) is the handle to the histogram, and h(2) is the handle to the density curve.