Histograms in MATLAB®

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


x = normrnd(5,1,100,1);

fig = figure;
hist(x)

fig2plotly(fig);
sd1 = 0.1;
mu1 = 0;
data1 = mu1+ sd1.*randn(5000,1);
sd2 = 0.1;
mu2 = 0;
data2 = mu2+ sd2.*randn(5000,1);

nbins = 50;
bound = 1;
bins = linspace(-bound,bound,nbins);

fig = figure;

y1 = hist(data1, bins);

y2 = hist(data2, bins);

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

xd = findobj('-property','XData');

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

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

fig2plotly();
Inspired from Matlab Forum.
sd1 = 0.1;
mu1 = 0;
data1 = mu1+ sd1.*randn(5000,1);
sd2 = 0.1;
mu2 = 0.3;
data2 = mu2+ sd2.*randn(5000,1);

nbins = 50;
bound = 1;
bins = linspace(-bound,bound,nbins);

fig = figure;

y1 = hist(data1, bins);

y2 = hist(data2, bins);

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

xd = findobj('-property','XData');

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

fig2plotly(fig);
sd = 0.1;
mu = 1;
data = mu + sd*randn(1,5000);

nbins = 50;

fig = figure;
[h1 bins]= hist(data,nbins);
barh(bins,h1);

fig2plotly(fig);
x = randn(500,1);

data = {...
  struct(...
    'x', x, ...
    'type', 'histogram')...
};

plotly(data);

y = randn(500,1);

data = {...
  struct(...
    'y', y, ...
    'type', 'histogram')...
};
plotly(data);

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');

plotly(data, struct('layout', layout));
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');
plotly(data, struct('layout', layout));
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);

plotly(data, struct('layout', layout));