Engee documentation
Notebook

histograms

In this demonstration, we will look at the histogram capabilities of Engee.

A histogram is a way of representing tabular data as a bar chart.

Quantitative ratios of some indicator are represented in the form of rectangles whose areas are proportional. Most often, for ease of perception, the width of the rectangles is equal, while their height determines the ratios of the displayed parameter.

Now let's move on to the implementation and start with connecting the visualisation library and consider the histogram function on the example of the simplest histogram visualisation using a vector of random numbers with normal distribution.

In [ ]:
using Plots
In [ ]:
x = randn(10^3)
histogram(x)
Out[0]:

The number of histogram columns by default is determined by the Friedman-Diakonis formula.

Alternatively, you can pass a range to the bins parameter to more precisely control the number of intervals, as well as their minimum and maximum. The wording "to plot 20 intervals from -5 to +5, enter ..." applies, and here we need to add 1 to the length, as the length takes into account the number of interval bounds.

In [ ]:
b_range = range(-5, 5, length=21)
plot(b_range, seriestype=:scatter)
Out[0]:

Normalisation

It is often necessary to normalise the histogram in some way. The normalise attribute is used for this purpose. It allows us to normalise the total area of the intervals to 1. Since we have selected a sample from a normal distribution, we can also plot it.

In [ ]:
p(x) = 1/sqrt(2pi) * exp(-x^2/2)

histogram(x, bins=b_range, normalize=:pdf, color=:gray)
plot!(p, lw=3, color=:red)
xlims!(-5, 5)
ylims!(0, 0.4)
Out[0]:

normalise can take on other values, including:

  1. :probability - summarises all cell heights up to 1;

  2. :density - the area of each container is equal to the quantity.

Weighted histograms

The next display option we will look at is weighted histograms.

In [ ]:
f_exp(x) = exp(x)/(exp(1)-1)

x = rand(10^4)
w = exp.(x)

histogram(x, bins=:scott, weights=w, normalize=:pdf, color=:gray)
plot!(f_exp, lw=3, color=:red)
plot!(legend=:topleft)
xlims!(0, 1.0)
ylims!(0, 1.6)
Out[0]:

The other options are.

  1. Histogram scatter plots can be plotted using scatterhist and scatterhist!, where columns are replaced by dots.
  2. Histogram scatter plots can be plotted using stephist and stephist!, where the outline replaces the columns.
In [ ]:
p1 = histogram(x, title="Bar")
p2 = scatterhist(x, title="Scatter")
p3 = stephist(x, title="Step")
plot(p1, p2, p3, layout=(1, 3), legend=false)
Out[0]:

Note: Note that the Y axis of the histogram scatter plot does not start at 0 by default.

Conclusion

In this example, we have looked at the possibilities of building various histograms in Engee and have seen how easy it is to work with this tool.