Mapbox Density Heatmap in F#

How to make a Mapbox Density Heatmap in F# with Plotly.


Plotly is known for its graphing libraries, but it's also a broader ecosystem for building interactive data apps, AI data analysis tools, and deploying and hosting Dash apps. Explore the full set of tools and platforms designed to help turn data into action. Discover Plotly.

Note: We are retiring documentation for R, MATLAB, Julia, and F#. Learn more about this change here.

In [1]:
#r "nuget: Plotly.NET,2.0.0-preview.8"
#r "nuget: Plotly.NET.Interactive,2.0.0-preview.8"
#r "nuget: FSharp.Data"
Installed Packages
  • FSharp.Data, 4.2.4
  • 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.

Mapbox Access Token and Base Map Configuration

To plot on Mapbox maps with Plotly you may need a Mapbox account and a public Mapbox Access Token.

Stamen Terrain base map (no token needed): density mapbox with Plotly.NET

In [2]:
open FSharp.Data
open Plotly.NET
open Plotly.NET.LayoutObjects

let data = CsvFile.Load("https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv")

let latitudes = data.Rows |> Seq.map (fun row -> row.GetColumn("Latitude"))
let longitudes = data.Rows |> Seq.map (fun row -> row.GetColumn("Longitude"))
let magnitudes = data.Rows |> Seq.map (fun row -> row.GetColumn("Magnitude"))

let lonlat = Seq.zip longitudes latitudes

Chart.DensityMapbox(lonlat=lonlat,Z=magnitudes,Radius=10.,Colorscale=StyleParam.Colorscale.Viridis)
|> Chart.withMapbox(Mapbox.init(Style=StyleParam.MapboxStyle.StamenTerrain))
|> Chart.withMarginSize (Left = 0, Right = 0, Top = 0, Bottom = 0)
Out[2]: