Histograms in F#
How to make Histograms in F# with Plotly.
In [1]:
#r "nuget: Plotly.NET, 2.0.0-preview.7"
#r "nuget: Plotly.NET.Interactive, 2.0.0-preview.7"
#r "nuget: newtonsoft.json"
open Plotly.NET
Basic Histogram¶
In [2]:
let N = 500
let rnd = System.Random()
let x = Array.init N (fun _ -> rnd.NextDouble())
Chart.Histogram(x)
Out[2]:
Normalized Histogram¶
In [3]:
let N = 500
let rnd = System.Random()
let x = Array.init N (fun _ -> rnd.NextDouble())
Chart.Histogram(
x,
HistNorm=StyleParam.HistNorm.Probability
)
Out[3]:
Horizontal Histogram¶
In [4]:
open Plotly.NET
let N = 500
let rnd = System.Random()
let x = Array.init N (fun _ -> rnd.NextDouble())
In [5]:
Chart.Histogram(
x,
Orientation=StyleParam.Orientation.Horizontal
) // issue with horizental allign
Out[5]:
Overlaid Histogram¶
In [6]:
let N = 500
let rnd = System.Random()
let x0 = Array.init N (fun _ -> rnd.NextDouble())
let x1= Array.init N (fun _ -> rnd.NextDouble())
[
Chart.Histogram(x0)
|> GenericChart.mapTrace(fun x ->
x.SetValue("name", "trace 0")
x.SetValue("marker", {| color = "rgba(103, 102, 255,1)"; line = {| color = "rgba(103, 102, 255, 1)"; width = 3 |} |})
x)
Chart.Histogram(x1)
|> GenericChart.mapTrace(fun x ->
x.SetValue("name", "trace 1")
x.SetValue("marker", {| color = "rgba(255, 70, 51, 1)"; line = {| color = "rgba(255, 70, 51, 1)"; width = 3 |} |})
x)
]
|> Chart.combine
|> Chart.withLayout(Layout.init(Barmode = StyleParam.Barmode.Overlay))
Out[6]:
Stacked Histograms¶
In [7]:
open Plotly.NET
let N = 500
let rnd = System.Random()
let x0 = Array.init N (fun _ -> rnd.NextDouble())
let x1= Array.init N (fun _ -> rnd.NextDouble())
[
Chart.Histogram(x0)
|> GenericChart.mapTrace(fun x ->
x.SetValue("name", "trace 0")
x.SetValue("marker", {| color = "rgba(103, 102, 255,1)"; line = {| color = "rgba(103, 102, 255, 1)"; width = 3 |} |})
x)
Chart.Histogram(x1)
|> GenericChart.mapTrace(fun x ->
x.SetValue("name", "trace 1")
x.SetValue("marker", {| color = "rgba(255, 70, 51, 1)"; line = {| color = "rgba(255, 70, 51, 1)"; width = 3 |} |})
x)
]
|> Chart.combine
|> Chart.withLayout(Layout.init(Barmode = StyleParam.Barmode.Stack))
Out[7]:
Styled Histogram¶
In [8]:
let N = 500
let rnd = System.Random()
let x0 = Array.init N (fun _ -> rnd.NextDouble())
let x1= Array.init N (fun _ -> rnd.NextDouble())
let layout =
let temp = Layout()
temp?barmode <- "Overlay"
temp?bargap <- 0.5
temp?bargroupgap <- 0.1
temp
[
Chart.Histogram(x0) // Issues setting the xbins start,end and size=0.5
|> GenericChart.mapTrace(fun x ->
x.SetValue("name", "control")
x.SetValue("HistNorm","percent")
x.SetValue("Opacity","0.75")
x.SetValue("marker", {| color = "rgba(255, 69, 246, 0.35)"; line = {| color = "rgba(255, 69, 246, 0.35)"; width = 5 |} |})
x)
Chart.Histogram(x1)
|> GenericChart.mapTrace(fun x ->
x.SetValue("name", "experimental")
x.SetValue("HistNorm","percent")
x.SetValue("Opacity","0.75")
x.SetValue("marker", {| color = "rgba(103, 102, 255,1)"; line = {| color = "rgba(103, 102, 255,1)"; width = 5 |} |})
x)
]
|> Chart.combine
|> Chart.withTitle("Sample Result")
|> Chart.withXAxisStyle("Value")
|> Chart.withYAxisStyle("Count")
//|> Chart.withLayout(Layout.init(Barmode = StyleParam.Barmode.Overlay))
|> Chart.withLayout(layout) // Issues setting the xbins start,end and size=0.5
Out[8]:
Cumulative Histogram¶
In [9]:
let N = 500
let rnd = System.Random()
let x = Array.init N (fun _ -> rnd.NextDouble())
In [10]:
Chart.Histogram(x) // issue with not find cumulative in histogram
Out[10]:
Specify Aggregation Function¶
In [11]:
let fruits = ["Apples";"Apples";"Apples";"Oranges"; "Bananas"];
let counts = ["5";"10";"3";"10";"5"]
[
Chart.Histogram(
fruits,
Name = "Sum"
);
Chart.Histogram(
counts ,
Name = "Count"
);
]
|> Chart.combine
Out[11]:
Share bins between histograms¶
In [12]:
let N = 500
let rnd = System.Random()
let x0 = Array.init N (fun _ -> rnd.Next(7, 20))
let x1 = Array.init N (fun _ -> rnd.Next(7, 20))
let layout =
let obj = Layout()
obj?bargap <- 0.5
obj?bargroupgap <- 0.1
obj
[
Chart.Histogram(x0)
|> GenericChart.mapTrace(fun x ->
x.SetValue("name", "trace 0")
x.SetValue("marker", {| color = "rgba(103, 102, 255, 1)"; line = {| color = "rgba(103, 102, 255, 1)"; width = 5 |} |})
x)
Chart.Histogram(x1)
|> GenericChart.mapTrace(fun x ->
x.SetValue("name", "trace 1")
x.SetValue("marker", {| color = "rgba(255, 70, 51, 1)"; line = {| color = "rgba(255, 70, 51, 1)"; width = 5 |} |})
x)
]
|> Chart.combine
|> Chart.withLayout(Layout.init(Barmode = StyleParam.Barmode.Stack))
Out[12]: