Skip to content

Commit 87e677e

Browse files
committed
Add histograms: 1d, 2d, 2dContour
1 parent cbe20a1 commit 87e677e

File tree

8 files changed

+386
-68
lines changed

8 files changed

+386
-68
lines changed

docs/content/histograms.fsx

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,58 @@ values.
1313
*)
1414

1515
open FSharp.Plotly
16-
17-
let values = [19; 26; 55;]
18-
let labels = ["Residential"; "Non-Residential"; "Utility"]
19-
20-
(*** define-output:pie1 ***)
21-
Chart.Pie(values,labels)
22-
(*** include-it:pie1 ***)
23-
24-
(*** define-output:doughnut1 ***)
25-
Chart.Doughnut(values,labels,hole=0.3)
26-
(*** include-it:doughnut1 ***)
16+
17+
18+
let normal (rnd:System.Random) mu tau =
19+
let mutable v1 = 2.0 * rnd.NextDouble() - 1.0
20+
let mutable v2 = 2.0 * rnd.NextDouble() - 1.0
21+
let mutable r = v1 * v1 + v2 * v2
22+
while (r >= 1.0 || r = 0.0) do
23+
v1 <- 2.0 * rnd.NextDouble() - 1.0
24+
v2 <- 2.0 * rnd.NextDouble() - 1.0
25+
r <- v1 * v1 + v2 * v2
26+
let fac = sqrt(-2.0*(log r)/r)
27+
(tau * v1 * fac + mu)
28+
29+
let rnd = System.Random()
30+
let sampleNormal () =
31+
normal (rnd) 0. 2.
32+
33+
let n = 2000
34+
let a = -1.
35+
let b = 1.2
36+
let step i = a + ((b - a) / float (n - 1)) * float i
37+
38+
let x = Array.init n (fun i -> ((step i)**3.) + (0.3 * sampleNormal () ))
39+
let y = Array.init n (fun i -> ((step i)**6.) + (0.3 * sampleNormal () ))
40+
41+
42+
let colorScale = StyleParam.Colorscale.Custom [(0.0,"white");(1.0,"red")]
43+
44+
(*** define-output:Histogram2dContour1 ***)
45+
[
46+
Chart.Histogram2dContour (x,y,Colorscale=colorScale,Line=Line.init(Width=0))
47+
Chart.Point(x,y,Opacity=0.3)
48+
]
49+
|> Chart.Combine
50+
(*** include-it:Histogram2dContour1 ***)
51+
|> Chart.Show
52+
53+
54+
(*** define-output:Histogram1 ***)
55+
[
56+
//var layout = {barmode: "overlay"};
57+
Chart.Histogram x
58+
Chart.Histogram y
59+
]
60+
|> Chart.Combine
61+
(*** include-it:Histogram1 ***)
62+
|> Chart.Show
63+
64+
65+
(*** define-output:Histogram2d1 ***)
66+
Chart.Histogram2d (x,y,Colorscale=colorScale)
67+
(*** include-it:Histogram2d1 ***)
68+
|> Chart.Show
69+
70+

src/FSharp.Plotly.WPF/FSharp.Plotly.WPF.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
<RootNamespace>FSharp.Plotly.WPF</RootNamespace>
1111
<AssemblyName>FSharp.Plotly.WPF</AssemblyName>
1212
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
13-
<TargetFSharpCoreVersion>4.4.0.0</TargetFSharpCoreVersion>
13+
<TargetFSharpCoreVersion>4.4.1.0</TargetFSharpCoreVersion>
1414
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1515
<Name>FSharp.Plotly.WPF</Name>
16+
<TargetFrameworkProfile />
1617
</PropertyGroup>
1718
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1819
<DebugSymbols>true</DebugSymbols>

src/FSharp.Plotly/Chart.fs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -377,20 +377,24 @@ type Chart =
377377
|> TraceStyle.TextLabel(?Text=Labels,?Textposition=TextPosition,?Textfont=TextFont)
378378
|> GenericChart.ofTraceObject
379379

380+
/// Computes a histogram with auto-determined the bin size.
381+
static member Histogram(data,?Orientation,?Name,?Showlegend,?Marker,?HistNorm,?HistFunc,?nBinsx,?nBinsy,?Xbins,?Ybins,?xError,?yError) =
382+
Trace.initHistogram (
383+
TraceStyle.Histogram (X=data,?Orientation=Orientation,?HistNorm=HistNorm,?HistFunc=HistFunc,
384+
?nBinsx=nBinsx,?nBinsy=nBinsy,?xBins=Xbins,?yBins=Ybins)
385+
)
386+
|> TraceStyle.TraceInfo(?Name=Name,?Showlegend=Showlegend)//,?Opacity=Opacity)
387+
|> GenericChart.ofTraceObject
380388

389+
/// Computes the bi-dimensional histogram of two data samples and auto-determines the bin size.
390+
static member Histogram2d(x,y,?Z,?Name,?Colorscale,?Showscale,?zSmooth,?Colorbar,?zAuto,?zMin,?zMax,?nBinsx,?nBinsy,?Xbins,?Ybins,?HistNorm,?HistFunc) =
391+
Trace.initHistogram2d (
392+
TraceStyle.Histogram2d (X=x, Y=y,? Z=Z,
393+
?Colorscale=Colorscale,?Showscale=Showscale,?zSmooth=zSmooth,?Colorbar=Colorbar) )
394+
|> TraceStyle.TraceInfo(?Name=Name)//,?Showlegend=Showlegend,?Opacity=Opacity)
395+
|> GenericChart.ofTraceObject
396+
381397

382-
// // /// Computes the bi-dimensional histogram of two data samples and auto-determines the bin size.
383-
// // static member Histogram2d(x,y,?Name,?HistNorm,?HistFunc,?Colorscale,?Showscale,?zSmooth,?Colorbar,?zAuto,?zMin,?zMax,?nBinsx,?nBinsy,?Xbins,?Ybins) =
384-
// // let trace =
385-
// // Trace()
386-
// // |> Helpers.ApplyTraceStyles("histogram2d",x=x,y=y,?name=Name,?histnorm=HistNorm,?histfunc=HistFunc,
387-
// // ?colorScale=Colorscale,?showscale=Showscale,?zsmooth=zSmooth,?colorbar=Colorbar,
388-
// // ?zauto=zAuto,?zmin=zMin,?zmax=zMax,
389-
// // ?nbinsx=nBinsx,?nbinsy=nBinsy,?xbins=Xbins,?ybins=Ybins
390-
// // )
391-
// //
392-
// // GenericChart.Chart (trace,None)
393-
// //
394398
// // /// Computes the bi-dimensional histogram of two data samples and auto-determines the bin size.
395399
// // static member Histogram2d(xy,?Name,?HistNorm,?HistFunc,?Colorscale,?Showscale,?zSmooth,?Colorbar,?zAuto,?zMin,?zMax,?nBinsx,?nBinsy,?Xbins,?Ybins) =
396400
// // let x,y = Seq.unzip xy
@@ -399,7 +403,13 @@ type Chart =
399403
// // ?nBinsx=nBinsx,?nBinsy=nBinsy,?Xbins=Xbins,?Ybins=Ybins
400404
// // )
401405

402-
406+
/// Computes the bi-dimensional histogram of two data samples and auto-determines the bin size.
407+
static member Histogram2dContour(x,y,?Z,?Name,?Colorscale,?Showscale,?Line,?zSmooth,?Colorbar,?zAuto,?zMin,?zMax,?nBinsx,?nBinsy,?Xbins,?Ybins,?HistNorm,?HistFunc) =
408+
Trace.initHistogram2dContour (
409+
TraceStyle.Histogram2dContour (X=x, Y=y,? Z=Z,?Line=Line,
410+
?Colorscale=Colorscale,?Showscale=Showscale,?zSmooth=zSmooth,?Colorbar=Colorbar) )
411+
//|> TraceStyle.TraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
412+
|> GenericChart.ofTraceObject
403413

404414

405415
// ---------------------------------------------------------------------------------------------------------------------------------------------------
@@ -422,7 +432,8 @@ type Chart =
422432

423433
/// Uses points, line or both depending on the mode to represent 3d-data points
424434
static member Surface(data:seq<#seq<#IConvertible>>,?X,?Y, ?Name,?Showlegend,?Opacity,?Contours,?Colorscale,?Showscale,?Colorbar) =
425-
Trace3d.initSurface (Trace3dStyle.Surface (Z=data,?X=X, ?Y=Y,?Contours=Contours,
435+
Trace3d.initSurface (
436+
Trace3dStyle.Surface (Z=data,?X=X, ?Y=Y,?Contours=Contours,
426437
?Colorscale=Colorscale,?Showscale=Showscale,?Colorbar=Colorbar ) )
427438
|> TraceStyle.TraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
428439
//|> TraceStyle.TextLabel(?Text=Labels,?Textposition=TextPosition,?Textfont=TextFont)

src/FSharp.Plotly/ChartExtensions.fs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,20 @@ module ChartExtensions =
156156

157157

158158

159-
// Sets z-Axis of 3d- Charts
160-
static member withZ_Axis(zAxis:Axis.LinearAxis) =
161-
(fun (ch:GenericChart) ->
162-
let layout =
163-
Layout()
164-
|> Layout.style(Scene=Scene.init(Scene.style (zAxis=zAxis) ))
165-
GenericChart.addLayout layout ch
166-
)
159+
//// Sets z-Axis of 3d- Charts
160+
//static member withZ_Axis(zAxis:Axis.LinearAxis) =
161+
// (fun (ch:GenericChart) ->
162+
// let layout =
163+
// Layout()
164+
// |> Layout.style(Scene=Scene.init(Scene.style (zAxis=zAxis) ))
165+
// GenericChart.addLayout layout ch
166+
// )
167167

168-
// Sets z-Axis style with ...
169-
static member withZ_AxisStyle(title,?MinMax,?Showgrid) =
170-
let range = if MinMax.IsSome then Some (StyleParam.RangeValues.MinMax (MinMax.Value)) else None
171-
let zaxis = Axis.LinearAxis.init(Axis.LinearAxis.LinearAxis(Title=title,?Range=range,?Showgrid=Showgrid))
172-
Chart.withZ_Axis(zaxis)
168+
//// Sets z-Axis style with ...
169+
//static member withZ_AxisStyle(title,?MinMax,?Showgrid) =
170+
// let range = if MinMax.IsSome then Some (StyleParam.RangeValues.MinMax (MinMax.Value)) else None
171+
// let zaxis = Axis.LinearAxis.init(Axis.LinearAxis.LinearAxis(Title=title,?Range=range,?Showgrid=Showgrid))
172+
// Chart.withZ_Axis(zaxis)
173173

174174

175175

src/FSharp.Plotly/Cumulative.fs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace FSharp.Plotly
2+
3+
4+
5+
//enabled (boolean)
6+
//If true, display the cumulative distribution by summing the binned values. Use the `direction` and `centralbin` attributes to tune the accumulation method. Note: in this mode, the "density" `histnorm` settings behave the same as their equivalents without "density": "" and "density" both rise to the number of data points, and "probability" and "probability density" both rise to the number of sample points.
7+
//direction ( enumerated : "increasing" | "decreasing" )
8+
//default: "increasing"
9+
//Only applies if cumulative is enabled. If "increasing" (default) we sum all prior bins, so the result increases from left to right. If "decreasing" we sum later bins so the result decreases from left to right.
10+
//currentbin ( enumerated : "include" | "exclude" | "half" )
11+
//default: "include"
12+
//Only applies if cumulative is enabled. Sets whether the current bin is included, excluded, or has half of its value included in the current cumulative value. "include" is the default for compatibility with various other tools, however it introduces a half-bin bias to the results. "exclude" makes the opposite half-bin bias, and "half" removes it.
13+
14+
15+
/// Cumulative type inherits from dynamic object
16+
type Cumulative () =
17+
inherit DynamicObj ()
18+
19+
// Init Cumulative()
20+
static member init
21+
(
22+
?Enabled : bool,
23+
?Direction : StyleParam.CumulativeDirection,
24+
?Currentbin : StyleParam.Currentbin
25+
) =
26+
Cumulative ()
27+
|> Cumulative.style
28+
(
29+
?Enabled = Enabled,
30+
?Direction = Direction ,
31+
?Currentbin = Currentbin
32+
)
33+
34+
35+
// Applies the styles to Cumulative()
36+
static member style
37+
(
38+
?Enabled,
39+
?Direction,
40+
?Currentbin
41+
) =
42+
43+
(fun (cumulative:('T :> Cumulative)) ->
44+
Enabled |> DynObj.setValueOpt cumulative "enabled"
45+
Direction |> DynObj.setValueOptBy cumulative "direction" StyleParam.CumulativeDirection.convert
46+
Currentbin |> DynObj.setValueOptBy cumulative "currentbin" StyleParam.Currentbin.convert
47+
48+
cumulative
49+
)

src/FSharp.Plotly/FSharp.Plotly.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<Compile Include="Font.fs" />
7070
<Compile Include="Axis.fs" />
7171
<Compile Include="Bins.fs" />
72+
<Compile Include="Cumulative.fs" />
7273
<Compile Include="Scene.fs" />
7374
<Compile Include="Shape.fs" />
7475
<Compile Include="Error.fs" />

src/FSharp.Plotly/StyleParams.fs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,30 @@ module StyleParam =
446446

447447
static member convert = Direction.toString >> box
448448

449+
/// Only applies if cumulative is enabled. If "increasing" (default) we sum all prior bins, so the result increases from left to right.
450+
/// If "decreasing" we sum later bins so the result decreases from left to right. default: "increasing"
451+
type CumulativeDirection =
452+
| Increasing | Decreasing
453+
454+
static member toString = function
455+
| Increasing -> "increasing"
456+
| Decreasing -> "decreasing"
457+
458+
static member convert = CumulativeDirection.toString >> box
459+
460+
/// Only applies if cumulative is enabled. Sets whether the current bin is included, excluded, or has half of its value included in
461+
/// the current cumulative value. "include" is the default for compatibility with various other tools, however it introduces
462+
/// a half-bin bias to the results. "exclude" makes the opposite half-bin bias, and "half" removes it.
463+
type Currentbin =
464+
| Include | Exclude | Half
465+
466+
static member toString = function
467+
| Include -> "Include"
468+
| Exclude -> "Exclude"
469+
| Half -> "Half"
470+
471+
static member convert = Currentbin.toString >> box
472+
449473

450474
/// Sets the type of normalization for this histogram trace. By default ('histnorm' set to '') the height of each bar
451475
/// displays the frequency of occurrence, i.e., the number of times this value was found in the corresponding bin.
@@ -455,10 +479,12 @@ module StyleParam =
455479
/// interval such that summing the area of all bins will yield the total number of occurrences. If set to 'probability density',
456480
/// the height of each bar is equal to the number of probability that an event will fall into the corresponding bin divided by
457481
/// the size of the bin interval such that summing the area of all bins will yield 1.
482+
/// default: None
458483
type HistNorm =
459-
| Percent | Probability | Density | ProbabilityDensity
484+
| None | Percent | Probability | Density | ProbabilityDensity
460485

461486
static member toString = function
487+
| None -> ""
462488
| Percent -> "percent"
463489
| Probability -> "probability"
464490
| Density -> "density"
@@ -470,6 +496,7 @@ module StyleParam =
470496
/// Sets the binning function used for this histogram trace. The default value is 'count' where the histogram values are computed
471497
/// by counting the number of values lying inside each bin. With 'histfunc' set to 'sum', 'avg', 'min' or 'max', the histogram values
472498
/// are computed using the sum, the average, the minimum or the 'maximum' of the values lying inside each bin respectively.
499+
/// default: Count
473500
type HistFunc =
474501
| Count | Sum | Avg | Min | Max
475502

@@ -615,6 +642,12 @@ module StyleParam =
615642

616643
static member convert = Delaunayaxis.toString >> box
617644

645+
618646

647+
// hoverinfo (flaglist string)
648+
//Any combination of "x", "y", "z", "text", "name" joined with a "+" OR "all" or "none" or "skip".
649+
//examples: "x", "y", "x+y", "x+y+z", "all"
650+
//default: "all"
651+
//Determines which trace information appear on hover. If `none` or `skip` are set, no information is displayed upon hovering. But, if `none` is set, click and hover events are still fired.
619652

620653

0 commit comments

Comments
 (0)