Skip to content

Commit 42fa161

Browse files
committed
Add: Contour plots
1 parent 5986281 commit 42fa161

File tree

4 files changed

+96
-296
lines changed

4 files changed

+96
-296
lines changed

docs/content/contour-plots.fsx

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,44 @@
33
#r "../../bin/FSharp.Plotly.dll"
44

55
(**
6-
# FSharp.Plotly: Pie and Doughnut Charts
6+
# FSharp.Plotly: Contour plot
77
8-
*Summary:* This example shows how to create pie and doughnut charts in F#.
8+
*Summary:* This example shows how to create contour plot in F#.
9+
10+
A contour plot is a graphical technique for representing a 3-dimensional surface by plotting
11+
constant z slices, called contours, on a 2-dimensional format. That is, given a value for z,
12+
lines are drawn for connecting the (x,y) coordinates where that z value occurs.
13+
The contour plot is an alternative to a 3-D surface plot.
14+
15+
The contour plot is an alternative to a 3-D surface plot.
916
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.
1317
*)
1418

19+
open System
1520
open FSharp.Plotly
21+
22+
let linspace (min,max,n) =
23+
if n <= 2 then failwithf "n needs to be larger then 2"
24+
let bw = float (max - min) / (float n - 1.)
25+
[|min ..bw ..max|]
1626

17-
let values = [19; 26; 55;]
18-
let labels = ["Residential"; "Non-Residential"; "Utility"]
27+
28+
let size = 100
29+
let x = linspace(-2. * Math.PI, 2. * Math.PI, size)
30+
let y = linspace(-2. * Math.PI, 2. * Math.PI, size)
31+
32+
let f x y = - (5. * x / (x**2. + y**2. + 1.) )
33+
34+
let z =
35+
Array.init size (fun i ->
36+
Array.init size (fun j -> f x.[j] y.[i] )
37+
)
38+
1939

20-
(*** define-output:pie1 ***)
21-
Chart.Pie(values,labels)
22-
(*** include-it:pie1 ***)
40+
(*** define-output:contour1 ***)
41+
z
42+
|> Chart.Contour
43+
|> Chart.withSize(600.,600.)
44+
(*** include-it:contour1 ***)
45+
2346

24-
(*** define-output:doughnut1 ***)
25-
Chart.Doughnut(values,labels,hole=0.3)
26-
(*** include-it:doughnut1 ***)

src/FSharp.Plotly/Chart.fs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,28 @@ type Chart =
235235

236236
GenericChart.Chart (trace,None)
237237

238-
/// Shows a graphical representation of data where the individual values contained in a matrix are represented as colors.
239-
static member HeatMap(data:seq<#seq<#IConvertible>>,?ColNames,?RowNames, ?Name,?Colorscale,?Showscale,?zSmooth,?Colorbar) =
238+
/// Shows a graphical representation of a 3-dimensional surface by plotting constant z slices, called contours, on a 2-dimensional format.
239+
/// That is, given a value for z, lines are drawn for connecting the (x,y) coordinates where that z value occurs.
240+
static member Heatmap(data:seq<#seq<#IConvertible>>,?ColNames,?RowNames, ?Name,?Colorscale,?Showscale,?zSmooth,?Colorbar) =
240241
let trace =
241242
Heatmap()
242-
|> Options.HeatMap(Z=data,?X=ColNames, ?Y=RowNames,
243+
|> Options.Colormap(Z=data,?X=ColNames, ?Y=RowNames,
243244
TraceOptions=Options.Trace(?Name=Name),
244245
?Colorscale=Colorscale,?Showscale=Showscale,?zSmooth=zSmooth,?Colorbar=Colorbar
245246
)
246247
GenericChart.Chart (trace,None)
247248

249+
/// Shows a graphical representation of data where the individual values contained in a matrix are represented as colors.
250+
static member Contour(data:seq<#seq<#IConvertible>>,?X,?Y, ?Name,?Colorscale,?Showscale,?zSmooth,?Colorbar) =
251+
let trace =
252+
Contour()
253+
|> Options.Colormap(Z=data,?X=X, ?Y=Y,
254+
TraceOptions=Options.Trace(?Name=Name),
255+
?Colorscale=Colorscale,?Showscale=Showscale,?zSmooth=zSmooth,?Colorbar=Colorbar
256+
)
257+
GenericChart.Chart (trace,None)
258+
259+
248260
/// Shows how proportions of data, shown as pie-shaped pieces, contribute to the data as a whole.
249261
static member Pie(values,?labels,?Name,?Showlegend,?Color,?Hoverinfo,?Textinfo,?Textposition) =
250262
let trace =

src/FSharp.Plotly/Options.fs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,8 @@ type Options() =
697697
)
698698

699699

700-
// Applies the styles to Heatmap()
701-
static member HeatMap
700+
// Applies the styles to Heatmap() and Contour()
701+
static member Colormap
702702
(
703703
?TraceOptions:TraceOptions<_>,
704704
?Z : seq<#seq<#IConvertible>>,
@@ -732,38 +732,38 @@ type Options() =
732732

733733

734734
) =
735-
(fun (heatMap:('T :> Heatmap)) ->
736-
737-
Z |> Option.iter heatMap.set_z
738-
X |> Option.iter heatMap.set_x
739-
X0 |> Option.iter heatMap.set_x0
740-
dX |> Option.iter heatMap.set_dx
741-
Y |> Option.iter heatMap.set_y
742-
Y0 |> Option.iter heatMap.set_y0
743-
dY |> Option.iter heatMap.set_dy
744-
Text |> Option.iter heatMap.set_text
745-
Transpose |> Option.iter heatMap.set_transpose
746-
xType |> Option.iter heatMap.set_xtype
747-
yType |> Option.iter heatMap.set_ytype
748-
zAuto |> Option.iter heatMap.set_zauto
749-
zMin |> Option.iter heatMap.set_zmin
750-
zMax |> Option.iter heatMap.set_zmax
751-
Colorscale |> Option.iter (StyleOption.ColorScale.convert >> heatMap.set_colorscale)
752-
Autocolorscale |> Option.iter heatMap.set_autocolorscale
753-
Reversescale |> Option.iter heatMap.set_reversescale
754-
Showscale |> Option.iter heatMap.set_showscale
755-
zSmooth |> Option.iter (StyleOption.SmoothAlg.convert >> heatMap.set_zsmooth)
756-
Connectgaps |> Option.iter heatMap.set_connectgaps
757-
Colorbar |> Option.iter heatMap.set_colorbar
758-
xAxis |> Option.iter heatMap.set_xaxis
759-
yAxis |> Option.iter heatMap.set_yaxis
760-
Zsrc |> Option.iter heatMap.set_zsrc
761-
Xsrc |> Option.iter heatMap.set_xsrc
762-
Ysrc |> Option.iter heatMap.set_ysrc
763-
Textsrc |> Option.iter heatMap.set_textsrc
735+
(fun (colorMap:('T :> Colormap)) ->
736+
737+
Z |> Option.iter colorMap.set_z
738+
X |> Option.iter colorMap.set_x
739+
X0 |> Option.iter colorMap.set_x0
740+
dX |> Option.iter colorMap.set_dx
741+
Y |> Option.iter colorMap.set_y
742+
Y0 |> Option.iter colorMap.set_y0
743+
dY |> Option.iter colorMap.set_dy
744+
Text |> Option.iter colorMap.set_text
745+
Transpose |> Option.iter colorMap.set_transpose
746+
xType |> Option.iter colorMap.set_xtype
747+
yType |> Option.iter colorMap.set_ytype
748+
zAuto |> Option.iter colorMap.set_zauto
749+
zMin |> Option.iter colorMap.set_zmin
750+
zMax |> Option.iter colorMap.set_zmax
751+
Colorscale |> Option.iter (StyleOption.ColorScale.convert >> colorMap.set_colorscale)
752+
Autocolorscale |> Option.iter colorMap.set_autocolorscale
753+
Reversescale |> Option.iter colorMap.set_reversescale
754+
Showscale |> Option.iter colorMap.set_showscale
755+
zSmooth |> Option.iter (StyleOption.SmoothAlg.convert >> colorMap.set_zsmooth)
756+
Connectgaps |> Option.iter colorMap.set_connectgaps
757+
Colorbar |> Option.iter colorMap.set_colorbar
758+
xAxis |> Option.iter colorMap.set_xaxis
759+
yAxis |> Option.iter colorMap.set_yaxis
760+
Zsrc |> Option.iter colorMap.set_zsrc
761+
Xsrc |> Option.iter colorMap.set_xsrc
762+
Ysrc |> Option.iter colorMap.set_ysrc
763+
Textsrc |> Option.iter colorMap.set_textsrc
764764

765765
// out ->
766-
heatMap |> (optApply TraceOptions)
766+
colorMap |> (optApply TraceOptions)
767767
)
768768

769769

0 commit comments

Comments
 (0)