Calculate histogram

histogram data nbins
(histogram data nbins min max norm)

Calculates histogram of data provided as a collection of values. Histogram is created with nbins and optionally can have range fixed at lower or upper limit with min and max values (use "auto as the value of min if only max is to be fixed). If norm is provided then histogram area is normalized to this value.

Histogram is an array of 4-element arrays, each corresponding to one bin in the histogram. Meanings of items in bin arrays are following: bin mid-x-value; bin height; x-error (half of the bin width); y-error (square root of the number of entries in the bin, optionally normalized).

POOL provides helper class plot_histo and factory function make_histo for creating histogram plots, both available in make_plots.l include file. See usage in the examples below.

Example 1:

"a := (rnorm 10 2 1000)
print (histogram :a 10 5 15)

Output:

{ {5.5 17 0.5 4.12310562561766}
{6.5 46 0.5 6.78232998312527}
{7.5 114 0.5 10.6770782520313}
{8.5 150 0.5 12.2474487139159}
{9.5 194 0.5 13.9283882771841}
{10.5 186 0.5 13.6381816969859}
{11.5 140 0.5 11.8321595661992}
{12.5 82 0.5 9.05538513813742}
{13.5 41 0.5 6.40312423743285}
{14.5 16 0.5 4} }

Example 2:

include "make_plots.l ;contains make_histo setrange and autoscale functions
"a := (rnorm 10 2 1000)
"h := (make_histo :a 10 5 15) ;10 bins, range 5-15

"cname := world @ :h ;read the graphics window name
print :cname

setrange :cname {0 20} {} ;set range (x axis only)
(autoscale "Histogram "y;automatically adjust scale (y axis only)

"cfg := thing word :cname "_canvas_cfg ;get canvas configuration
:cfg,"min_y := 0

wait 1000
(set_nbins 20) @ :h ;change histogram binning

Output:

histogram

Example 3:

include "make_plots.l ;contains class plot_histo and function setrange

to bars :d :n :c [:min 0] [:max 10] ;bars class, based on plot_histo
  use (plot_histo :d :n :min :max 1)
  :style,"xerr := "none             ;do not show x-error bars
  setti "bar                        ;set bar style of the plot
  setr (:max - :min) / :n           ;radius is used to set bar width
  (settc :c 20)

  to resize :new_n
    setr (:max - :min) / :new_n ;adjust bar width
    set_nbins :new_n ;set new binning (base class function)
  end
end

"j := 5000  ;number of entries in the histogram
"k := 3     ;number of histograms
"h := []    ;list of histograms
repeat :k [ ;create histogram plots (in parallel)
  push :h (anewp "pool $bars (rnorm repcount repcount/2 :j) 100 100*repcount)
]

setrange "pool {0 10} {0 1}

"s := (slider "|n =| {70 4} {10 200 1} 100)
setonchange :s [(resize getvalue :s) @ :h;change binning (all histograms in parallel)
ht

Output:

Histogram plots, the number of bins is changed with the slider.

See also:

Table of Content