Histograms
Plot histograms of a data set, used for visualizing the distribution of the data.
The examples correspond to https://alandefreitas.github.io/matplotplusplus/plot-types/data-distribution/histogram/.
Examples
- A simple histogram.
::
var m: Random = Random(0)
let x = randn(m, 10000, 0.0, 1.0)
var h1 = hist(x)
Result:
.. image:: ../../../tests/imgs/histogram/histogram_1.png :align: center :width: 360
- Comparison of different binning algorithms.
::
var m: Random = Random(0)
let x = randn(m, 10000, 0.0, 1.0)
subplot(2, 3, 0)
hist(x, algorithm:BinningAlgorithm.Automatic)
title("Automatic binning")
subplot(2, 3, 1)
hist(x, algorithm:Scott)
title("Scott's rule")
subplot(2, 3, 2)
hist(x, algorithm:FD)
title("Freedman-Diaconis rule")
subplot(2, 3, 3)
hist(x, algorithm:Integers)
title("Integers rule")
subplot(2, 3, 4)
hist(x, algorithm:Sturges)
title("Sturges' rule")
subplot(2, 3, 5)
hist(x, algorithm:Sqrt)
title("Square root rule")
Result:
.. image:: ../../../tests/imgs/histogram/histogram_2.png :align: center :width: 360
- Comparison of different normalization methods.
::
var m: Random = Random(0)
let x = randn(m, 10000, 0.0, 1.0)
subplot(2, 3, 0)
hist(x, normalization_alg:Count)
title("Count (c_i)")
subplot(2, 3, 1)
hist(x, normalization_alg:Probability)
title("Probability (c_i/N)")
subplot(2, 3, 2)
hist(x, normalization_alg:CumulativeCount)
title("Cummulative count (∑_{j=1}^i c_j)")
subplot(2, 3, 3)
hist(x, normalization_alg:CountDensity)
title("Count density (c_i/w_i)")
subplot(2, 3, 4)
hist(x, normalization_alg:Pdf)
title("PDF (c_i/(N w_i))")
subplot(2, 3, 5)
hist(x, normalization_alg:Cdf)
title("CDF (∑_{j=1}^i c_j/N)")
Result:
.. image:: ../../../tests/imgs/histogram/histogram_3.png :align: center :width: 360
- Test for values and bin edges.
::
var m: Random = Random(0)
let x = randn(m, 1000, 0.0, 1.0)
let num_bins = 25
let handle = hist(x, num_bins)
println(handle.values())
println(handle.bin_edges())
Result:
.. image:: ../../../tests/imgs/histogram/histogram_4.png :align: center :width: 360
- Query for the number of bins.
::
var m: Random = Random(0)
let x = randn(m, 1000, 0.0, 1.0)
let handle = hist(x)
title(handle.num_bins().toString() + " bins")
Result:
.. image:: ../../../tests/imgs/histogram/histogram_5.png :align: center :width: 360
- Custom setting of bin edges.
::
var m: Random = Random(0)
let x = randn(m, 10000, 0.0, 1.0)
let edges = vector<Float64>([
-10.0, -2.0, -1.75, -1.5, -1.25, -1.0,
-0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75,
1.0, 1.25, 1.5, 1.75, 2.0, 10.0
])
hist(x, edges).normalization(Normalization.CountDensity)
Result:
.. image:: ../../../tests/imgs/histogram/histogram_6.png :align: center :width: 360
- Plotting distribution of categories.
::
let categories = [
"no", "no", "yes", "yes", "yes", "no", "no",
"no", "no", "undecided", "undecided", "yes", "no", "no",
"no", "yes", "no", "yes", "no", "yes", "no",
"no", "no", "yes", "yes", "yes", "yes"
]
hist(categories).bar_width(Float32(0.5))
Result:
.. image:: ../../../tests/imgs/histogram/histogram_7.png :align: center :width: 360
- Illustration for probability mode.
::
var m: Random = Random(0)
let x = randn(m, 10000, 0.0, 1.0)
let handle = hist(x, normalization_alg:Normalization.Probability)
println(sum(handle.values())) // should be 1.0
Result:
.. image:: ../../../tests/imgs/histogram/histogram_8.png :align: center :width: 360
- Plotting two distributions (with different number of samples) in the probability mode.
::
var m: Random = Random(0)
let x = randn(m, 2000, 0.0, 1.0)
let y = randn(m, 5000, 1.0, 1.0)
let handle1 = hist(x)
hold(true)
let handle2 = hist(y)
handle1.normalization(Normalization.Probability)
handle1.bin_width(0.25)
handle2.normalization(Normalization.Probability)
handle2.bin_width(0.25)
Result:
.. image:: ../../../tests/imgs/histogram/histogram_9.png :align: center :width: 360
- Customize face color and edge color.
::
var m: Random = Random(0)
let x = randn(m, 1000, 0.0, 1.0)
let handle = hist(x)
handle.num_bins(15)
handle.bin_edges(vector([-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0]))
handle.face_color(0.0, 0.0, 0.5, 0.5)
handle.edge_color("r")
Result:
.. image:: ../../../tests/imgs/histogram/histogram_10.png :align: center :width: 360
- Plotting pdf of a distribution along with a histogram of samples.
::
var m: Random = Random(0)
let x = randn(m, 5000, 5.0, 2.0)
let handle = hist(x)
handle.normalization(Normalization.Pdf)
hold(true)
fplot({x => normalPDF(x, 5.0, 2.0)}, -5.0, 15.0).line_width(1.5)
Result:
.. image:: ../../../tests/imgs/histogram/histogram_11.png :align: center :width: 360
- Customize number of bins.
::
var m: Random = Random(0)
let x = randn(m, 1000, 0.0, 1.0)
let handle = hist(x)
handle.num_bins(5)
title(handle.num_bins().toString() + " bins")
Result:
.. image:: ../../../tests/imgs/histogram/histogram_12.png :align: center :width: 360
Methods
======================================== ================================= Method Description ======================================== ================================= hist(x, algorithm, normalization_alg) Plot a histogram. ======================================== =================================
Description
.. function:: hist(x, algorithm!, normalization_alg!)
:param x: Tensor<Float64>
:param algorithm: BinningAlgorithm
:param normalization_alg: Normalization
Plot a histogram.