// can't yet format YamlFrontmatter (["title: 3D surface plots"; "category: 3D Charts"; "categoryindex: 4"; "index: 3"], Some { StartLine = 2 StartColumn = 0 EndLine = 6 EndColumn = 8 }) to pynb markdown

In [ ]:
#r "nuget: Plotly.NET, 2.0.0-preview.6"
#r "nuget: Plotly.NET.Interactive, 2.0.0-preview.6"


# 3D surface plots¶

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:

In [3]:
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

In [ ]:
surface

Out[ ]:
In [5]:
// 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))

In [ ]:
surface2

Out[ ]: