Histogram with a distribution fit
collapse all in page
Syntax
histfit(data)
histfit(data,nbins)
histfit(data,nbins,dist)
histfit(ax,___)
h = histfit(___)
Description
example
histfit(data)
plotsa histogram of values in data
using the numberof bins equal to the square root of the number of elements in data
andfits a normal density function.
example
histfit(data,nbins)
plotsa histogram using nbins
bins and fits a normaldensity function.
example
histfit(data,nbins,dist)
plotsa histogram with nbins
bins and fits a densityfunction from the distribution specified by dist
.
example
histfit(ax,___)
uses the plot axes specified by the Axes
object ax
. Specify ax
as the first input argument followed by any of the input argument combinations in the previous syntaxes.
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.
Examples
collapse all
Histogram with a Normal Distribution Fit
Open Live Script
Generate a sample of size 100 from a normal distribution with mean 10 and variance 1.
rng default; % For reproducibilityr = normrnd(10,1,100,1);
Construct a histogram with a normal distribution fit.
histfit(r)
histfit
uses fitdist
to fit a distribution to data. Use fitdist
to obtain parameters used in fitting.
pd = fitdist(r,'Normal')
pd = NormalDistribution Normal distribution mu = 10.1231 [9.89244, 10.3537] sigma = 1.1624 [1.02059, 1.35033]
The intervals next to the parameter estimates are the 95% confidence intervals for the distribution parameters.
Histogram for a Given Number of Bins
Open Live Script
Generate a sample of size 100 from a normal distribution with mean 10 and variance 1.
rng default; % For reproducibilityr = 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
Open Live Script
Generate a sample of size 100 from a beta distribution with parameters (3,10).
rng default; % For reproducibilityb = 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
Open Live Script
Generate a sample of size 100 from a beta distribution with parameters (3,10).
rng default; % For reproducibilityb = betarnd(3,10,[100,1]);
Construct a histogram using 10 bins with a smoothing function fit.
histfit(b,10,'kernel')
Specify Axes for Histogram with Distribution Fit
Open Live Script
Generate a sample of size 100
from a normal distribution with mean 3
and variance 1
.
rng('default') % For reproducibilityr = normrnd(3,1,100,1);
Create a figure with two subplots and return the Axes
objects as ax1
and ax2
. Create a histogram with a normal distribution fit in each set of axes by referring to the corresponding Axes
object. In the left subplot, plot a histogram with 10 bins. In the right subplot, plot a histogram with 5 bins. Add a title to each plot by passing the corresponding Axes
object to the title
function.
ax1 = subplot(1,2,1); % Left subplothistfit(ax1,r,10,'normal')title(ax1,'Left Subplot')ax2 = subplot(1,2,2); % Right subplothistfit(ax2,r,5,'normal')title(ax2,'Right Subplot')
Handle for a Histogram with a Distribution Fit
Open Live Script
Generate a sample of size 100 from a normal distribution with mean 10 and variance 1.
rng default % for reproducibilityr = normrnd(10,1,100,1);
Construct a histogram with a normal distribution fit.
h = histfit(r,10,'normal')
h = 2x1 graphics array: Bar 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 data
vector
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 bins
positive 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 adistribution.
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) | character vector | string scalar
Distribution to fit to the histogram, specified as a character vector or string scalar. The following table shows the supported distributions.
dist | Description |
---|---|
'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. |
ax
— Axes for plot
Axes
object
Axes for the plot, specified as an Axes
object. If you do not specify ax
, then histfit
creates the plot using the current axes. For more information on creating an Axes
object, see axes.
Output Arguments
collapse all
h
— Handles for the plot
plot 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. histfit
normalizes the density to match the total area under the curve with that of the histogram.
Algorithms
histfit
uses fitdist to fit a distribution to data. Use fitdist
to obtain parameters used in fitting.
Version History
Introduced before R2006a
See Also
histogram | normfit | distributionFitter | fitdist | paramci
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office