# Histograms in MATLAB®

How to make Histogram plots in MATLAB® with Plotly.

## Histogram of Vector

Generate 10,000 random numbers and create a histogram. The histogram function automatically chooses an appropriate number of bins to cover the range of values in x and show the shape of the underlying distribution.

x = randn(10000,1);
h = histogram(x);

fig2plotly(gcf);


When you specify an output argument to the histogram function, it returns a histogram object. You can use this object to inspect the properties of the histogram, such as the number of bins or the width of the bins.

Find the number of histogram bins.

x = randn(10000,1);
h = histogram(x);
nbins = h.NumBins

nbins =

38


## Specify Number of Histogram Bins

Plot a histogram of 1,000 random numbers sorted into 25 equally spaced bins.

x = randn(1000,1);
nbins = 25;
h = histogram(x,nbins);

fig2plotly(gcf);


Find the bin counts.

x = randn(1000,1);
nbins = 25;
h = histogram(x,nbins);

counts = h.Values

counts =

Columns 1 through 13

1     0     1     3     8     6     9    26    29    42    57    77   107

Columns 14 through 25

106   103   122    85    77    53    34    24    13    12     3     2


## Change Number of Histogram Bins

Generate 1,000 random numbers and create a histogram.

X = randn(1000,1);
h = histogram(X);

fig2plotly(gcf);


Use the morebins function to coarsely adjust the number of bins.

X = randn(1000,1);
h = histogram(X);

Nbins = morebins(h);

fig2plotly(gcf);


Adjust the bins at a fine grain level by explicitly setting the number of bins.

X = randn(1000,1);
h = histogram(X);

Nbins = morebins(h);

h.NumBins = 31;

fig2plotly(gcf);


## Specify Bin Edges of Histogram

Generate 1,000 random numbers and create a histogram. Specify the bin edges as a vector with wide bins on the edges of the histogram to capture the outliers that do not satisfy |x|<2. The first vector element is the left edge of the first bin, and the last vector element is the right edge of the last bin.

x = randn(1000,1);
edges = [-10 -2:0.25:2 10];
h = histogram(x,edges);

fig2plotly(gcf);


Specify the Normalization property as 'countdensity' to flatten out the bins containing the outliers. Now, the area of each bin (rather than the height) represents the frequency of observations in that interval.

x = randn(1000,1);
edges = [-10 -2:0.25:2 10];
h = histogram(x,edges);

h.Normalization = 'countdensity';

fig2plotly(gcf);


## Plot Categorical Histogram

Create a categorical vector that represents votes. The categories in the vector are 'yes', 'no', or 'undecided'.

A = [0 0 1 1 1 0 0 0 0 NaN NaN 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1];
C = categorical(A,[1 0 NaN],{'yes','no','undecided'});


Plot a categorical histogram of the votes, using a relative bar width of 0.5.

A = [0 0 1 1 1 0 0 0 0 NaN NaN 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1];
C = categorical(A,[1 0 NaN],{'yes','no','undecided'});

h = histogram(C,'BarWidth',0.5);

fig2plotly(gcf);


## Histogram with Specified Normalization

Generate 1,000 random numbers and create a histogram using the 'probability' normalization.

x = randn(1000,1);
h = histogram(x,'Normalization','probability');

fig2plotly(gcf);


Compute the sum of the bar heights. With this normalization, the height of each bar is equal to the probability of selecting an observation within that bin interval, and the height of all of the bars sums to 1.

x = randn(1000,1);
h = histogram(x,'Normalization','probability');

S = sum(h.Values);

fig2plotly(gcf);


## Plot Multiple Histograms

Generate two vectors of random numbers and plot a histogram for each vector in the same figure.

x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);

fig2plotly(gcf);


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.

x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);

h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h2.Normalization = 'probability';
h2.BinWidth = 0.25;

fig2plotly(gcf);


Generate 1,000 random numbers and create a histogram. Return the histogram object to adjust the properties of the histogram without recreating the entire plot.

x = randn(1000,1);
h = histogram(x);

fig2plotly(gcf);


Specify exactly how many bins to use.

x = randn(1000,1);
h = histogram(x);

h.NumBins = 15;

fig2plotly(gcf);


Specify the edges of the bins with a vector. The first value in the vector is the left edge of the first bin. The last value is the right edge of the last bin.

x = randn(1000,1);
h = histogram(x);

h.NumBins = 15;

h.BinEdges = [-3:3];

fig2plotly(gcf);


Change the color of the histogram bars.

x = randn(1000,1);
h = histogram(x);

h.NumBins = 15;

h.BinEdges = [-3:3];

h.FaceColor = [0 0.5 0.5];
h.EdgeColor = 'r';

fig2plotly(gcf);


## Determine Underlying Probability Distribution

Generate 5,000 normally distributed random numbers with a mean of 5 and a standard deviation of 2. Plot a histogram with Normalization set to 'pdf' to produce an estimation of the probability density function.

x = 2*randn(5000,1) + 5;
histogram(x,'Normalization','pdf');

fig2plotly(gcf);


In this example, the underlying distribution for the normally distributed data is known. You can, however, use the 'pdf' histogram plot to determine the underlying probability distribution of the data by comparing it against a known probability density function.

The probability density function for a normal distribution with mean μ, standard deviation σ, and variance σ2 is

 f(x,μ,σ)=1σG2π exp[−(x−μ)22σ2]. 

Overlay a plot of the probability density function for a normal distribution with a mean of 5 and a standard deviation of 2.

x = 2*randn(5000,1) + 5;
histogram(x,'Normalization','pdf');

hold on
y = -5:0.1:15;
mu = 5;
sigma = 2;
f = exp(-(y-mu).^2./(2*sigma^2))./(sigma*sqrt(2*pi));
plot(y,f,'LineWidth',1.5)

fig2plotly(gcf);


Use the savefig function to save a histogram figure.

histogram(randn(10));
savefig('histogram.fig');
close gcf


Use openfig to load the histogram figure back into MATLAB. openfig also returns a handle to the figure, h.

h = openfig('histogram.fig');

fig2plotly(gcf);


Use the findobj function to locate the correct object handle from the figure handle. This allows you to continue manipulating the original histogram object used to generate the figure.

h = openfig('histogram.fig');

y = findobj(h,'type','histogram');

fig2plotly(gcf);