-
Notifications
You must be signed in to change notification settings - Fork 81
/
4_4_2d-histograms.fsx
88 lines (70 loc) · 2.35 KB
/
4_4_2d-histograms.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(**
---
title: 2D Histograms
category: Distribution Charts
categoryindex: 5
index: 4
---
*)
(*** hide ***)
(*** condition: prepare ***)
#r "nuget: Newtonsoft.JSON, 12.0.3"
#r "../bin/Plotly.NET/netstandard2.0/Plotly.NET.dll"
(*** condition: ipynb ***)
#if IPYNB
#r "nuget: Plotly.NET, {{fsdocs-package-version}}"
#r "nuget: Plotly.NET.Interactive, {{fsdocs-package-version}}"
#endif // IPYNB
(**
# 2D Histograms
[![Binder]({{root}}img/badge-binder.svg)](https://mybinder.org/v2/gh/plotly/Plotly.NET/gh-pages?filepath={{fsdocs-source-basename}}.ipynb) 
[![Script]({{root}}img/badge-script.svg)]({{root}}{{fsdocs-source-basename}}.fsx) 
[![Notebook]({{root}}img/badge-notebook.svg)]({{root}}{{fsdocs-source-basename}}.ipynb)
*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:
*)
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.
*)
let histogramContour =
[
Chart.Histogram2dContour (x,y,Line=Line.init(Width=0.))
Chart.Point(x,y,Opacity=0.3)
]
|> Chart.Combine
(*** condition: ipynb ***)
#if IPYNB
histogramContour
#endif // IPYNB
(***hide***)
histogramContour |> GenericChart.toChartHTML
(*** include-it-raw ***)
let histogram2d =
Chart.Histogram2d (x,y)
(*** condition: ipynb ***)
#if IPYNB
histogram2d
#endif // IPYNB
(***hide***)
histogram2d |> GenericChart.toChartHTML
(*** include-it-raw ***)