3D surface plots

BinderScriptNotebook

Summary: This example shows how to create 3D surface plots 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.)
    Array.init n (fun i -> min + (bw * float i))

//---------------------- 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] )
                    )

let rnd = System.Random()
let a = Array.init 50 (fun _ -> rnd.NextDouble())
let b = Array.init 50 (fun _ -> rnd.NextDouble())
let c = Array.init 50 (fun _ -> rnd.NextDouble())

let surface = 
    z
    |> Chart.Surface
// Create simple example data were x y and z is given (z is a xy-Matrix)
let x' = [0.;2.5]
let y' = [0.;2.5]
let z' = [
    [1.;1.;]; // row wise (length x)
    [1.;2.;];
    ] // column (length y)

let surface2 = 
    Chart.Surface(z',x',y',Opacity=0.5,Contours=Contours.initXyz(Show=true))
namespace System
namespace Plotly
namespace Plotly.NET
val linspace : min:float * max:float * n:int -> float []
val min : float
val max : float
val n : int
val failwithf : format:Printf.StringFormat<'T,'Result> -> 'T
val bw : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

--------------------
[<Struct>]
type float = Double

--------------------
type float<'Measure> =
  float
type Array =
  interface ICollection
  interface IEnumerable
  interface IList
  interface IStructuralComparable
  interface IStructuralEquatable
  interface ICloneable
  new : unit -> unit
  member Clone : unit -> obj
  member CopyTo : array: Array * index: int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  ...
val init : count:int -> initializer:(int -> 'T) -> 'T []
val i : int
val size : int
val x : float []
type Math =
  static member Abs : value: decimal -> decimal + 6 overloads
  static member Acos : d: float -> float
  static member Acosh : d: float -> float
  static member Asin : d: float -> float
  static member Asinh : d: float -> float
  static member Atan : d: float -> float
  static member Atan2 : y: float * x: float -> float
  static member Atanh : d: float -> float
  static member BigMul : a: int * b: int -> int64 + 2 overloads
  static member BitDecrement : x: float -> float
  ...
field Math.PI: float = 3.14159265359
val y : float []
val f : x:float -> y:float -> float
val x : float
val y : float
val z : float [] []
val j : int
val rnd : Random
Multiple items
type Random =
  new : unit -> unit + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer: byte [] -> unit + 1 overload
  member NextDouble : unit -> float
  member Sample : unit -> float

--------------------
Random() : Random
Random(Seed: int) : Random
val a : float []
Random.NextDouble() : float
val b : float []
val c : float []
val surface : GenericChart.GenericChart
type Chart =
  static member Area : x:seq<#IConvertible> * y:seq<#IConvertible> * ?Name:string * ?ShowMarkers:bool * ?Showlegend:bool * ?MarkerSymbol:Symbol * ?Color:string * ?Opacity:float * ?Labels:seq<#IConvertible> * ?TextPosition:TextPosition * ?TextFont:Font * ?Dash:DrawingStyle * ?Width:float -> GenericChart + 1 overload
  static member Bar : keys:seq<#IConvertible> * values:seq<#IConvertible> * ?Name:string * ?Showlegend:bool * ?Color:string * ?Opacity:float * ?Labels:seq<#IConvertible> * ?TextPosition:TextPosition * ?TextFont:Font * ?Marker:Marker -> GenericChart + 1 overload
  static member BoxPlot : ?x:'a0 * ?y:'a1 * ?Name:string * ?Showlegend:bool * ?Color:string * ?Fillcolor:'a2 * ?Opacity:float * ?Whiskerwidth:'a3 * ?Boxpoints:Boxpoints * ?Boxmean:BoxMean * ?Jitter:'a4 * ?Pointpos:'a5 * ?Orientation:Orientation * ?Marker:Marker * ?Line:Line * ?Alignmentgroup:'a6 * ?Offsetgroup:'a7 * ?Notched:bool * ?NotchWidth:float * ?QuartileMethod:QuartileMethod -> GenericChart + 1 overload
  static member Bubble : x:seq<#IConvertible> * y:seq<#IConvertible> * sizes:seq<#IConvertible> * ?Name:string * ?Showlegend:bool * ?MarkerSymbol:Symbol * ?Color:string * ?Opacity:float * ?Labels:seq<#IConvertible> * ?TextPosition:TextPosition * ?TextFont:Font * ?StackGroup:string * ?Orientation:Orientation * ?GroupNorm:GroupNorm * ?UseWebGL:bool -> GenericChart + 1 overload
  static member Candlestick : open:seq<#IConvertible> * high:seq<#IConvertible> * low:seq<#IConvertible> * close:seq<#IConvertible> * x:seq<#IConvertible> * ?Increasing:Line * ?Decreasing:Line * ?WhiskerWidth:float * ?Line:Line * ?XCalendar:Calendar -> GenericChart + 1 overload
  static member ChoroplethMap : locations:seq<string> * z:seq<#IConvertible> * ?Text:seq<#IConvertible> * ?Locationmode:LocationFormat * ?Autocolorscale:bool * ?Colorscale:Colorscale * ?Colorbar:Colorbar * ?Marker:Marker * ?GeoJson:'a2 * ?FeatureIdKey:string * ?Zmin:float * ?Zmax:float -> GenericChart
  static member ChoroplethMapbox : locations:seq<#IConvertible> * z:seq<#IConvertible> * geoJson:'a2 * ?FeatureIdKey:string * ?Text:seq<#IConvertible> * ?Below:string * ?Colorscale:Colorscale * ?Colorbar:Colorbar * ?ZAuto:bool * ?ZMin:float * ?ZMid:float * ?ZMax:float -> GenericChart
  static member Column : keys:seq<#IConvertible> * values:seq<#IConvertible> * ?Name:string * ?Showlegend:bool * ?Color:string * ?Opacity:float * ?Labels:seq<#IConvertible> * ?TextPosition:TextPosition * ?TextFont:Font * ?Marker:Marker -> GenericChart + 1 overload
  static member Contour : data:seq<#seq<'a1>> * ?X:seq<#IConvertible> * ?Y:seq<#IConvertible> * ?Name:string * ?Showlegend:bool * ?Opacity:float * ?Colorscale:Colorscale * ?Showscale:'a4 * ?zSmooth:SmoothAlg * ?Colorbar:'a5 -> GenericChart (requires 'a1 :> IConvertible)
  static member DensityMapbox : lon:seq<#IConvertible> * lat:seq<#IConvertible> * ?Z:seq<#IConvertible> * ?Radius:float * ?Opacity:float * ?Text:seq<#IConvertible> * ?Below:string * ?Colorscale:Colorscale * ?Colorbar:Colorbar * ?Showscale:bool * ?ZAuto:bool * ?ZMin:float * ?ZMid:float * ?ZMax:float -> GenericChart + 1 overload
  ...
static member Chart.Surface : data:seq<#seq<'b>> * ?X:seq<#IConvertible> * ?Y:seq<#IConvertible> * ?Name:string * ?Showlegend:bool * ?Opacity:float * ?Contours:'e * ?Colorscale:StyleParam.Colorscale * ?Showscale:bool * ?Colorbar:'f -> GenericChart.GenericChart (requires 'b :> IConvertible)
module GenericChart

from Plotly.NET
val toChartHTML : gChart:GenericChart.GenericChart -> string
val x' : float list
val y' : float list
val z' : float list list
val surface2 : GenericChart.GenericChart
Multiple items
type Contours =
  inherit DynamicObj
  new : unit -> Contours
  static member init : ?X:Contour * ?Y:Contour * ?Z:Contour -> Contours
  static member initXyz : ?Show:bool * ?Color:string * ?UseColorMap:bool * ?Width:float * ?Highlight:bool * ?Highlightcolor:string * ?Highlightwidth:float -> Contours
  static member style : ?X:Contour * ?Y:Contour * ?Z:Contour -> (Contours -> Contours)
  static member styleXyz : ?Show:bool * ?Color:string * ?UseColorMap:bool * ?Width:float * ?Highlight:bool * ?Highlightcolor:string * ?Highlightwidth:float -> (Contours -> Contours)

--------------------
new : unit -> Contours
static member Contours.initXyz : ?Show:bool * ?Color:string * ?UseColorMap:bool * ?Width:float * ?Highlight:bool * ?Highlightcolor:string * ?Highlightwidth:float -> Contours