-
Notifications
You must be signed in to change notification settings - Fork 81
/
04_3_contour-plots.fsx
125 lines (95 loc) · 2.82 KB
/
04_3_contour-plots.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(**
---
title: Contour plots
category: Distribution Charts
categoryindex: 5
index: 4
---
*)
(*** hide ***)
(*** condition: prepare ***)
#r "nuget: Newtonsoft.JSON, 13.0.1"
#r "nuget: DynamicObj, 1.0.1"
#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
(**
# Contour plots
[![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 contour plot in F#.
let's first create some data for the purpose of creating example charts:
*)
open System
open Plotly.NET
// Generate linearly spaced vector
let linspace (min,max,n) =
if n <= 2 then failwithf "n needs to be larger then 2"
let bw = float (max - min) / (float n - 1.)
[|min ..bw ..max|]
// Create example data
let size = 100
let x = linspace(-2. * Math.PI, 2. * Math.PI, size)
let y = linspace(-2. * Math.PI, 2. * Math.PI, size)
let f x y = - (5. * x / (x**2. + y**2. + 1.) )
let z =
Array.init size (fun i ->
Array.init size (fun j ->
f x.[j] y.[i]
)
)
(**
A contour plot is a graphical technique for representing a 3-dimensional surface by plotting
constant z slices, called contours, on a 2-dimensional format. That is, given a value for z,
lines are drawn for connecting the (x,y) coordinates where that z value occurs.
The contour plot is an alternative to a 3-D surface plot.
*)
let contour1 =
z
|> Chart.Contour
|> Chart.withSize(600.,600.)
(*** condition: ipynb ***)
#if IPYNB
contour1
#endif // IPYNB
(***hide***)
contour1 |> GenericChart.toChartHTML
(***include-it-raw***)
(**
## Smooth Contour Coloring
to apply heatmap gradient coloring between each contour level, set the `ContourColoring` to `heatmap`:
*)
let contour2 =
Chart.Contour(
z,
ContoursColoring = StyleParam.ContourColoring.Heatmap
)
(*** condition: ipynb ***)
#if IPYNB
contour2
#endif // IPYNB
(***hide***)
contour2 |> GenericChart.toChartHTML
(***include-it-raw***)
(**
## Contour Line Labels
Use `ContourLabelFont` to set a contour label font, and display the labels with `ShowContourLabels`:
*)
let contour3 =
Chart.Contour(
z,
ContoursColoring = StyleParam.ContourColoring.Heatmap,
ShowContourLabels = true,
ContourLabelFont = Font.init(Size = 12., Color = Color.fromKeyword White)
)
(*** condition: ipynb ***)
#if IPYNB
contour3
#endif // IPYNB
(***hide***)
contour3 |> GenericChart.toChartHTML
(***include-it-raw***)