# Distplots in MATLAB®

How to make Distplots in MATLAB® with Plotly.

## Histogram with a Normal Distribution Fit

Construct a histogram with a normal distribution fit.

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

fig2plotly()


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

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

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

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

fig2plotly()


## Histogram with a Specified Distribution Fit

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

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

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

fig2plotly()


## Histogram with a Kernel Smoothing Function Fit

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

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

fig2plotly()


## Specify Axes for Histogram with Distribution Fit

Generate a sample of size 100 from a normal distribution with mean 3 and variance 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.

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

ax1 = subplot(1,2,1); % Left subplot
histfit(ax1,r,10,'normal')
title(ax1,'Left Subplot')

ax2 = subplot(1,2,2); % Right subplot
histfit(ax2,r,5,'normal')
title(ax2,'Right Subplot')

fig2plotly()


## Handle for a Histogram with a Distribution Fit

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

Construct a histogram with a normal distribution fit.

rng default % for reproducibility
r = normrnd(10,1,100,1);
h = histfit(r,10,'normal')

fig2plotly()


Change the bar colors of the histogram.

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

fig2plotly()


Change the color of the density curve.

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

fig2plotly()