histogram

Single vector input

x = rand(1000, 1)
histogram(x)

Specifying number of bins

options = struct
options.nbins = 5
x = randn(1000, 1)
histogram(x,options)

Specifying bin edges

options = struct
options.edges = [-5 -2:0.3:2 6]
x = randn(1000,1)
histogram(x, options)

Probability distribution normalization

options = struct
options.norm = 'probability'
x = poissrnd(4, 300, 1)
histogram(x, options)

Specifying bar properties

options = struct
options.barFill = '#DCDCDC'
options.barWidth = 20
options.barStroke = '#808080'
options.barStrokeWidth = 2
options.norm = 'probability'
options.nbins = 10
x = poissrnd(4, 300, 1)
histogram(x,options)

Defining ticks and labels

options = struct
options.xAxisTicks = 2
options.xAxisLabels = {'yes', 'no', 'absent'}
x = [1 0 1 0 0 0 1 2 0 1 ]
histogram(x, options)

Overlaying histograms

op1 = struct('show', false)
op2 = op1
op1.barWidth = 20
op2.show = false
op2.barFill = 'lightgreen'
op2.barWidth = 20
op2.barOpacity = 0.50
x = randn(3000,1)
y = -2 + randn(4000,1)
s1 = histogram(x, op1)
s2 = histogram(y, op2)
overlay({s1, s2})

Overlaying histogram and plot

op1 = struct('show', false)
op2 = op1
y = randn(1000,1)
op1.barFill =  '#DCDCDC'
op1.nbins = 20
s1 = histogram(y,op1)
x = linspace(min(y),max(y),1000)
op2.markerSize = 0
op2.yAxisMin = 0
op2.lineStroke = 'red'
op2.dashArray = '6 3'
op2.lineWidth = 1
s2 = scatter(x,1000*0.3/sqrt(2*pi)*exp(-x.^2/2),op2)
overlay({s1, s2})

Horizontal histogram

x = [2 6 5 3 4 4 5 3 4 1]
options = struct
options.horizontal = true
histogram(x,options)

See also

bar | area | plot