Heatmaps in Fsharp

How to make Heatmaps in F# with Plotly.


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

#r "nuget: Deedle"
#r "nuget: FSharp.Data"
Installed Packages
  • Deedle, 2.4.3
  • FSharp.Data, 4.2.3
  • Plotly.NET, 2.0.0-preview.8
  • Plotly.NET.Interactive, 2.0.0-preview.8

Loading extensions from Plotly.NET.Interactive.dll

Added Kernel Extension including formatters for Plotly.NET charts.

Basic Heatmap

In [2]:
open Plotly.NET

let matrix =
    [[1.;1.5;0.7;2.7];
    [2.;0.5;1.2;1.4];
    [0.1;2.6;2.4;3.0];]

let rownames = ["p3";"p2";"p1"]
let colnames = ["Tp0";"Tp30";"Tp60";"Tp160"]

let colorscaleValue =
    StyleParam.Colorscale.Custom [(0.0,"#3D9970");(1.0,"#001f3f")]

Chart.Heatmap(
    data=matrix,ColNames=colnames,RowNames=rownames,
    Colorscale=colorscaleValue,
    Showscale=true
)
|> Chart.withSize(700.,500.)
|> Chart.withMarginSize(Left=200.)
Out[2]:
In [3]:
open Deedle
open FSharp.Data

let volcano =
    Http.RequestString "https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv"
    |> fun csv -> Frame.ReadCsvString(csv,true,separators=",")

Chart.Heatmap(data=Frame.toJaggedArray(volcano))
Out[3]:

Sequential Colorscales: Greys

In [4]:
open Deedle
open FSharp.Data

let volcano =
    Http.RequestString "https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv"
    |> fun csv -> Frame.ReadCsvString(csv,true,separators=",")

Chart.Heatmap(data=Frame.toJaggedArray(volcano),Colorscale=StyleParam.Colorscale.Greys)
Out[4]:

Custom colorscales

In [5]:
open Deedle
open FSharp.Data

let volcano =
    Http.RequestString "https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv"
    |> fun csv -> Frame.ReadCsvString(csv,true,separators=",")

let customColorscale = StyleParam.Colorscale.Custom [(0.0,"red");(1.0,"green")]

Chart.Heatmap(data=Frame.toJaggedArray(volcano),Colorscale=customColorscale)
Out[5]: