|
3 | 3 | #r "../../bin/FSharp.Plotly.dll" |
4 | 4 |
|
5 | 5 | (** |
6 | | -# FSharp.Plotly: Pie and Doughnut Charts |
| 6 | +# FSharp.Plotly: Histogram2d |
7 | 7 |
|
8 | | -*Summary:* This example shows how to create pie and doughnut charts in F#. |
| 8 | +*Summary:* This example shows how to create a one-dimensional histogram of a data samples in F#. |
9 | 9 |
|
10 | | -A pie or a doughnut chart can be created using the `Chart.Pie` and `Chart.Doughnut` functions. |
11 | | -When creating pie or doughnut charts, it is usually desirable to provide both labels and |
12 | | -values. |
| 10 | +A histogram consisting of rectangles whose area is proportional to the frequency of a variable and whose width is equal to the class interval. |
| 11 | +The histogram chart represents the distribution of numerical data and can be created using the `Chart.Histogram`. |
13 | 12 | *) |
14 | 13 |
|
15 | 14 | open FSharp.Plotly |
16 | 15 |
|
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 | 16 | 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 ***) |
| 17 | +let x = [for i=0 to 500 do yield rnd.NextDouble() ] |
| 18 | + |
| 19 | +(*** define-output:histo1 ***) |
| 20 | +x |
| 21 | +|> Chart.Histogram |
| 22 | +|> Chart.withSize(500.,500.) |
| 23 | +//(*** include-it:histo1 ***) |
51 | 24 | |> Chart.Show |
52 | 25 |
|
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 | | - |
0 commit comments