-
Notifications
You must be signed in to change notification settings - Fork 81
/
05_1_geo-plots.fsx
150 lines (124 loc) · 3.92 KB
/
05_1_geo-plots.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(**
---
title: Scatter and line plots on Geo maps
category: Geo map charts
categoryindex: 6
index: 2
---
*)
(*** hide ***)
(*** condition: prepare ***)
#r "nuget: Newtonsoft.JSON, 13.0.1"
#r "nuget: DynamicObj, 2.0.0"
#r "nuget: Giraffe.ViewEngine, 1.4.0"
#r "../src/Plotly.NET/bin/Release/netstandard2.0/Plotly.NET.dll"
Plotly.NET.Defaults.DefaultDisplayOptions <-
Plotly.NET.DisplayOptions.init (PlotlyJSReference = Plotly.NET.PlotlyJSReference.NoReference)
(*** condition: ipynb ***)
#if IPYNB
#r "nuget: Plotly.NET, {{fsdocs-package-version}}"
#r "nuget: Plotly.NET.Interactive, {{fsdocs-package-version}}"
#endif // IPYNB
(**
# Scatter and line plots on Geo maps
[![Binder]({{root}}img/badge-binder.svg)](https://mybinder.org/v2/gh/plotly/plotly.net/gh-pages?urlpath=/tree/home/jovyan/{{fsdocs-source-basename}}.ipynb) 
[![Notebook]({{root}}img/badge-notebook.svg)]({{root}}{{fsdocs-source-basename}}.ipynb)
*Summary:* This example shows how to create Point and Line charts on geo maps in F#.
let's first create some data for the purpose of creating example charts:
*)
open Plotly.NET
let cityNames =
[ "Montreal"
"Toronto"
"Vancouver"
"Calgary"
"Edmonton"
"Ottawa"
"Halifax"
"Victoria"
"Winnepeg"
"Regina" ]
let lon =
[ -73.57
-79.24
-123.06
-114.1
-113.28
-75.43
-63.57
-123.21
-97.13
-104.6 ]
let lat = [ 45.5; 43.4; 49.13; 51.1; 53.34; 45.24; 44.64; 48.25; 49.89; 50.45 ]
(**
The simplest type of geo plot is plotting the (lon,lat) pairs of a location via `Chart.PointGeo`.
Here is an example using the location of Canadian cities:
*)
open Plotly.NET.LayoutObjects
let pointGeo =
Chart.PointGeo(
longitudes = lon,
latitudes = lat,
MultiText = cityNames,
TextPosition = StyleParam.TextPosition.TopCenter
)
|> Chart.withGeoStyle (
Scope = StyleParam.GeoScope.NorthAmerica,
Projection = GeoProjection.init (StyleParam.GeoProjectionType.AzimuthalEqualArea),
CountryColor = Color.fromString "lightgrey"
)
|> Chart.withMarginSize (0, 0, 0, 0)
(*** condition: ipynb ***)
#if IPYNB
pointGeo
#endif // IPYNB
(***hide***)
pointGeo |> GenericChart.toChartHTML
(***include-it-raw***)
(**
To connect the given (lon,lat) pairs via straight lines, use `Chart.LineGeo`.
Below is an example that pulls external data as a Deedle data
frame containing the source and target locations of American Airlines flights from Feb. 2011:
*)
#r "nuget: Deedle"
#r "nuget: FSharp.Data"
open Deedle
open FSharp.Data
open System.IO
open System.Text
let data =
Http.RequestString
"https://raw.githubusercontent.com/plotly/datasets/c34aaa0b1b3cddad335173cb7bc0181897201ee6/2011_february_aa_flight_paths.csv"
|> fun csv -> Frame.ReadCsvString(csv, true, separators = ",")
let opacityVals: float[] =
data.["cnt"]
|> Series.values
|> fun s -> s |> Seq.map (fun v -> v / (Seq.max s)) |> Array.ofSeq
let startCoords = Series.zipInner data.["start_lon"] data.["start_lat"]
let endCoords = Series.zipInner data.["end_lon"] data.["end_lat"]
let coords = Series.zipInner startCoords endCoords |> Series.values
let flights =
coords
|> Seq.mapi (fun i (startCoords, endCoords) ->
Chart.LineGeo(
lonlat = [ startCoords; endCoords ],
Opacity = opacityVals.[i],
MarkerColor = Color.fromString "red"
))
|> Chart.combine
|> Chart.withLegend (false)
|> Chart.withGeoStyle (
Scope = StyleParam.GeoScope.NorthAmerica,
Projection = GeoProjection.init (StyleParam.GeoProjectionType.AzimuthalEqualArea),
ShowLand = true,
LandColor = Color.fromString "lightgrey"
)
|> Chart.withMarginSize (0, 0, 50, 0)
|> Chart.withTitle "Feb. 2011 American Airline flights"
(*** condition: ipynb ***)
#if IPYNB
flights
#endif // IPYNB
(***hide***)
flights |> GenericChart.toChartHTML
(***include-it-raw***)