// can't yet format YamlFrontmatter (["title: 2D Histograms"; "category: Distribution Charts"; "categoryindex: 5"; "index: 4"], Some { StartLine = 2 StartColumn = 0 EndLine = 6 EndColumn = 8 }) to pynb markdown

In [ ]:
#r "nuget: Plotly.NET, 2.0.0-preview.16"
#r "nuget: Plotly.NET.Interactive, 2.0.0-preview.16"

2D Histograms

BinderScriptNotebook

Summary: This example shows how to create a bi-dimensional histogram of two data samples in F#.

let's first create some data for the purpose of creating example charts:

In [3]:
open Plotly.NET 

//---------------------- generate random normally distributed data ---------------------- 
let normal (rnd:System.Random) mu tau =
    let mutable v1 = 2.0 * rnd.NextDouble() - 1.0
    let mutable v2 = 2.0 * rnd.NextDouble() - 1.0
    let mutable r = v1 * v1 + v2 * v2
    while (r >= 1.0 || r = 0.0) do
        v1 <- 2.0 * rnd.NextDouble() - 1.0
        v2 <- 2.0 * rnd.NextDouble() - 1.0
        r <- v1 * v1 + v2 * v2
    let fac = sqrt(-2.0*(log r)/r)
    (tau * v1 * fac + mu)

let rnd = System.Random()
let n = 2000
let a = -1.
let b = 1.2
let step i = a +  ((b - a) / float (n - 1)) * float i

//---------------------- generate data distributed in x and y direction ---------------------- 
let x = Array.init n (fun i -> ((step i)**3.) + (0.3 * (normal (rnd) 0. 2.) ))
let y = Array.init n (fun i -> ((step i)**6.) + (0.3 * (normal (rnd) 0. 2.) ))

A Histogram2D chart can be created using the Chart.Histogram2D or Chart.Histogram2DContour functions.

In [4]:
let histogramContour =
    [
        Chart.Histogram2DContour (x,y,Line=Line.init(Width=0.))
        Chart.Point(x,y,Opacity=0.3)
    ]
    |> Chart.combine
In [ ]:
histogramContour
Out[ ]:
In [6]:
let histogram2D = 
    Chart.Histogram2D (x,y)
In [ ]:
histogram2D
Out[ ]: