Show Sidebar Hide Sidebar

# Histograms in MATLAB

How to make a histogram in MATLAB. Seven examples of colored, horizontal, and normal histogram bar charts.

#### Basic Histogram with MATLAB's `hist` Function

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% some random points
x = normrnd(5,1,100,1);

% a simple histogram
fig = figure;
hist(x)

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-basic-histogram');
plotly_url = response.url;
``````

#### Stacked Histogram with the `hist` Function

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% stacked histogram
sd1 = 0.1;
mu1 = 0;
data1 = mu1+ sd1.*randn(5000,1);
sd2 = 0.1;
mu2 = 0;
data2 = mu2+ sd2.*randn(5000,1);

% bin specs.
nbins = 50;
bound = 1;
bins = linspace(-bound,bound,nbins);

fig = figure;

% first histogram
y1 = hist(data1, bins);

% second histogram
y2 = hist(data2, bins);

% stacked histograms
bar([y1.' y2.'],'stacked')

% relabelx-axis range/ticks
xd = findobj('-property','XData');

for i=1:2
dat = get(xd(i),'XData');
dat = 2*dat/nbins - bound;
set(xd(i),'XData',dat);
end

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-stacked-histogram');
plotly_url = response.url;
``````

#### Histogram with Line

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

x = 5+2*randn(1000,1);
h = histc(x,(0:9));
stem(h,'b')

response = fig2plotly;
plotly_url = response.url;``````
Inspired from Matlab Forum.

#### Overlaid Histogram with `hist`

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% overlaid histogram
sd1 = 0.1;
mu1 = 0;
data1 = mu1+ sd1.*randn(5000,1);
sd2 = 0.1;
mu2 = 0.3;
data2 = mu2+ sd2.*randn(5000,1);
%bin specs.
nbins = 50;
bound = 1;
bins = linspace(-bound,bound,nbins);

fig = figure;

% first histogram
y1 = hist(data1, bins);
% second histogram
y2 = hist(data2, bins);

% overlay histograms
bar(y1.');
hold on;
bar(y2.','r');

% relabel x-axis range/ticks
xd = findobj('-property','XData');

for i=1:2
dat = get(xd(i),'XData');
dat = 2*dat/nbins - bound;
set(xd(i),'XData',dat);
end

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-overlaid-histogram');
plotly_url = response.url;
``````

#### Horizontal Histogram using `hist` and `bar`

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% horizontal histogram
sd = 0.1;
mu = 1;
data = mu + sd*randn(1,5000);

% bin specs.
nbins = 50;

% make/plot histogram of data
fig = figure;
[h1 bins]= hist(data,nbins);
barh(bins,h1);

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-horizontal-histogram');
plotly_url = response.url;
``````

#### Basic Histogram

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

x = randn(500,1);

data = {...
struct(...
'x', x, ...
'type', 'histogram')...
};
response = plotly(data, struct('filename', 'basic-histogram', 'fileopt', 'overwrite'));
plot_url = response.url
``````

#### Horizontal Histogram

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

y = randn(500,1);

data = {...
struct(...
'y', y, ...
'type', 'histogram')...
};
response = plotly(data, struct('filename', 'horizontal-histogram', 'fileopt', 'overwrite'));
plot_url = response.url
``````

#### Overlaid Histgram

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

x0 = randn(500,1);
x1 = randn(500,1)+1;

trace1 = struct(...
'x', x0, ...
'opacity', 0.75, ...
'type', 'histogram');
trace2 = struct(...
'x', x1, ...
'opacity', 0.75, ...
'type', 'histogram');
data = {trace1, trace2};
layout = struct('barmode', 'overlay');
response = plotly(data, struct('layout', layout, 'filename', 'overlaid-histogram', 'fileopt', 'overwrite'));
plot_url = response.url
``````

#### Stacked Histograms

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

x0 = randn(500,1);
x1 = randn(500,1)+1;

trace1 = struct(...
'x', x0, ...
'type', 'histogram');
trace2 = struct(...
'x', x1, ...
'type', 'histogram');
data = {trace1, trace2};
layout = struct('barmode', 'stack');
response = plotly(data, struct('layout', layout, 'filename', 'stacked-histogram', 'fileopt', 'overwrite'));
plot_url = response.url
``````

#### Colored and Styled Histograms

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

x0 = randn(500,1);
x1 = randn(500,1)+1;

trace1 = struct(...
'x', x0, ...
'histnorm', 'count', ...
'name', 'control', ...
'autobinx', false, ...
'xbins', struct(...
'start', -3.2, ...
'end', 2.8, ...
'size', 0.2), ...
'marker', struct(...
'color', 'fuchsia', ...
'line', struct(...
'color', 'grey', ...
'width', 0), ...
'opacity', 0.75), ...
'type', 'histogram');
trace2 = struct(...
'x', x1, ...
'name', 'experimental', ...
'autobinx', false, ...
'xbins', struct(...
'start', -1.8, ...
'end', 4.2, ...
'size', 0.2), ...
'marker', struct('color', 'rgb(255, 217, 102)'), ...
'opacity', 0.75, ...
'type', 'histogram');
data = {trace1, trace2};
layout = struct(...
'title', 'Sampled Results', ...
'xaxis', struct('title', 'Value'), ...
'yaxis', struct('title', 'Count'), ...
'barmode', 'overlay', ...
'bargap', 0.25, ...
'bargroupgap', 0.3);
response = plotly(data, struct('layout', layout, 'filename', 'style-histogram', 'fileopt', 'overwrite'));
plot_url = response.url
``````