Skip to content

Commit 8533cbb

Browse files
committed
#265: Add Chart.PointDensity plus tests/docs
1 parent 5da1b8b commit 8533cbb

File tree

7 files changed

+244
-11
lines changed

7 files changed

+244
-11
lines changed

Plotly.NET.sln

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{7B09CC0A-F
7979
docs\04_3_contour-plots.fsx = docs\04_3_contour-plots.fsx
8080
docs\04_4_2d-histograms.fsx = docs\04_4_2d-histograms.fsx
8181
docs\04_5_splom.fsx = docs\04_5_splom.fsx
82+
docs\04_6_point-density.fsx = docs\04_6_point-density.fsx
8283
docs\05_0_geo-vs-mapbox.fsx = docs\05_0_geo-vs-mapbox.fsx
8384
docs\05_1_geo-plots.fsx = docs\05_1_geo-plots.fsx
8485
docs\05_2_choropleth-map.fsx = docs\05_2_choropleth-map.fsx

docs/04_4_2d-histograms.fsx

+2-8
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,7 @@ let y = Array.init n (fun i -> ((step i)**6.) + (0.3 * (normal (rnd) 0. 2.) ))
6060
A Histogram2D chart can be created using the `Chart.Histogram2D` or `Chart.Histogram2DContour` functions.
6161
*)
6262

63-
let histogramContour =
64-
[
65-
Chart.Histogram2DContour (x,y,ContourLine=Line.init(Width=0.))
66-
Chart.Point(x,y,Opacity=0.3)
67-
]
68-
|> Chart.combine
63+
let histogramContour = Chart.Histogram2DContour (x,y,ContourLine=Line.init(Width=0.))
6964

7065
(*** condition: ipynb ***)
7166
#if IPYNB
@@ -76,8 +71,7 @@ histogramContour
7671
histogramContour |> GenericChart.toChartHTML
7772
(*** include-it-raw ***)
7873

79-
let histogram2D =
80-
Chart.Histogram2D (x,y)
74+
let histogram2D = Chart.Histogram2D (x,y)
8175

8276
(*** condition: ipynb ***)
8377
#if IPYNB

docs/04_6_point-density.fsx

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
(**
2+
---
3+
title: PointDensity
4+
category: Distribution Charts
5+
categoryindex: 5
6+
index: 6
7+
---
8+
*)
9+
10+
(*** hide ***)
11+
12+
(*** condition: prepare ***)
13+
#r "nuget: Newtonsoft.JSON, 13.0.1"
14+
#r "nuget: DynamicObj, 1.0.1"
15+
#r "../bin/Plotly.NET/netstandard2.0/Plotly.NET.dll"
16+
17+
(*** condition: ipynb ***)
18+
#if IPYNB
19+
#r "nuget: Plotly.NET, {{fsdocs-package-version}}"
20+
#r "nuget: Plotly.NET.Interactive, {{fsdocs-package-version}}"
21+
#endif // IPYNB
22+
23+
(**
24+
# PointDensity
25+
26+
[![Binder]({{root}}img/badge-binder.svg)](https://mybinder.org/v2/gh/plotly/Plotly.NET/gh-pages?filepath={{fsdocs-source-basename}}.ipynb) 
27+
[![Script]({{root}}img/badge-script.svg)]({{root}}{{fsdocs-source-basename}}.fsx) 
28+
[![Notebook]({{root}}img/badge-notebook.svg)]({{root}}{{fsdocs-source-basename}}.ipynb)
29+
30+
*Summary:* This example shows how to create PointDensity plots in F#.
31+
32+
let's first create some data for the purpose of creating example charts:
33+
34+
*)
35+
36+
let rnd = new System.Random()
37+
let x = [for i in 0 .. 100 -> rnd.NextDouble()]
38+
let y = [for i in 0 .. 100 -> rnd.NextDouble()]
39+
40+
(**
41+
`Chart.PointDensity` is a combination of a scatter plot and a histogram2dcontour.
42+
43+
It helps assessing the 2 dimensional distribution of a scatter plot by adding density contours based on the same data.
44+
*)
45+
46+
open Plotly.NET
47+
48+
let pointDensityChart =
49+
Chart.PointDensity(x,y)
50+
51+
(*** condition: ipynb ***)
52+
#if IPYNB
53+
pointDensityChart
54+
#endif // IPYNB
55+
56+
(***hide***)
57+
pointDensityChart |> GenericChart.toChartHTML
58+
(***include-it-raw***)
59+
60+
(**
61+
## More styled example
62+
63+
This example shows the usage of some of the styling possibility using `Chart.PointDensity`.
64+
*)
65+
66+
let pointDensityChartStyled =
67+
Chart.PointDensity(
68+
x,
69+
y,
70+
PointMarkerColor = Color.fromKeyword Purple,
71+
PointMarkerSymbol = StyleParam.MarkerSymbol.X,
72+
PointMarkerSize = 4,
73+
ColorScale = StyleParam.Colorscale.Viridis,
74+
ColorBar = ColorBar.init(Title = Title.init("Density")),
75+
ShowContourLabels = true
76+
)
77+
78+
(*** condition: ipynb ***)
79+
#if IPYNB
80+
pointDensityChartStyled
81+
#endif // IPYNB
82+
83+
(***hide***)
84+
pointDensityChartStyled |> GenericChart.toChartHTML
85+
(***include-it-raw***)

src/Plotly.NET.ImageExport/PuppeteerSharpRenderer.fs

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type PuppeteerSharpRenderer() =
8181
let fetchAndLaunchBrowserAsync () =
8282
async {
8383
match PuppeteerSharpRendererOptions.localBrowserExecutablePath with
84-
| None ->
84+
| None ->
8585
use browserFetcher = new BrowserFetcher()
8686

8787
let! revision = browserFetcher.DownloadAsync() |> Async.AwaitTask
@@ -95,9 +95,9 @@ type PuppeteerSharpRenderer() =
9595
| Some p ->
9696
let launchOptions =
9797
PuppeteerSharpRendererOptions.launchOptions
98-
98+
9999
launchOptions.ExecutablePath <- p
100-
100+
101101
return! Puppeteer.LaunchAsync(launchOptions) |> Async.AwaitTask
102102
}
103103

src/Plotly.NET/ChartAPI/Chart.fs

+1
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,7 @@ type Chart =
12271227
?BackgroundColor = BackgroundColor,
12281228
?ShowBackground = ShowBackground
12291229
)
1230+
12301231
Chart.withYAxis (yaxis, ?Id = Id)
12311232

12321233

src/Plotly.NET/ChartAPI/Chart2D.fs

+111
Original file line numberDiff line numberDiff line change
@@ -5018,3 +5018,114 @@ module Chart2D =
50185018
?ShowUpperHalf = ShowUpperHalf,
50195019
?UseDefaults = UseDefaults
50205020
)
5021+
5022+
/// <summary>
5023+
/// Creates a point density plot - a combination of a Scatter plot and Histogram2DContour.
5024+
///
5025+
/// Additionally to plotting the (x,y) data as points on a 2D plane, a density contour plot is computed by grouping a set of points specified by their x and y coordinates into bins, and applying a count aggregation function to compute the value to be used to compute contours.
5026+
/// The sample data from which statistics are computed is set in `x` and `y` (where `x` and `y` represent marginal distributions, binning is set in `xbins` and `ybins` in this case). The resulting distribution is visualized as a contour plot.
5027+
///
5028+
/// </summary>
5029+
/// <param name="x">Sets the x coordinates of the plotted data as well as the sample data to be binned on the x axis.</param>
5030+
/// <param name="y">Sets the y coordinates of the plotted data as well as the sample data to be binned on the y axis.</param>
5031+
/// <param name="PointOpacity">Sets the opacity of the point trace.</param>
5032+
/// <param name="PointMarkerColor">Sets the marker color of the point trace.</param>
5033+
/// <param name="PointMarkerSymbol">Sets the marker symbol of the point trace.</param>
5034+
/// <param name="PointMarkerSize">Sets the marker size of the point trace.</param>
5035+
/// <param name="ContourLineColor">Sets the color of the contour lines of the histogram2dcontour trace.</param>
5036+
/// <param name="ContourLineSmoothing">Sets the smoothing of the contour lines of the histogram2dcontour trace.</param>
5037+
/// <param name="ContourLineWidth">Sets the width of the contour lines of the histogram2dcontour trace.</param>
5038+
/// <param name="ShowContourLines">Wether or not to show contour lines</param>
5039+
/// <param name="ShowContourLabels">Wether or not to show contour labels</param>
5040+
/// <param name="ContourColoring">Determines the coloring method showing the contour values. If "fill", coloring is done evenly between each contour level If "heatmap", a heatmap gradient coloring is applied between each contour level. If "lines", coloring is done on the contour lines. If "none", no coloring is applied on this trace.</param>
5041+
/// <param name="NContours">Sets the maximum number of contour levels. The actual number of contours will be chosen automatically to be less than or equal to the value of `ncontours`. Has an effect only if `autocontour` is "true" or if `contours.size` is missing.</param>
5042+
/// <param name="HistNorm">Specifies the type of normalization used for this histogram trace. If "", the span of each bar corresponds to the number of occurrences (i.e. the number of data points lying inside the bins). If "percent" / "probability", the span of each bar corresponds to the percentage / fraction of occurrences with respect to the total number of sample points (here, the sum of all bin HEIGHTS equals 100% / 1). If "density", the span of each bar corresponds to the number of occurrences in a bin divided by the size of the bin interval (here, the sum of all bin AREAS equals the total number of sample points). If "probability density", the area of each bar corresponds to the probability that an event will fall into the corresponding bin (here, the sum of all bin AREAS equals 1).</param>
5043+
/// <param name="ContourOpacity">Sets the opacity of the histogram2dcontour trace.</param>
5044+
/// <param name="ColorBar">Sets the color bar.</param>
5045+
/// <param name="ColorScale">Sets the colorscale of the histogram2dcontour trace.</param>
5046+
/// <param name="ShowScale">wether or not to show the colorbar</param>
5047+
/// <param name="UseDefaults">If set to false, ignore the global default settings set in `Defaults`</param>
5048+
[<Extension>]
5049+
static member PointDensity
5050+
(
5051+
x: seq<#IConvertible>,
5052+
y: seq<#IConvertible>,
5053+
?PointOpacity: float,
5054+
?PointMarkerColor: Color,
5055+
?PointMarkerSymbol: StyleParam.MarkerSymbol,
5056+
?PointMarkerSize: int,
5057+
?ContourLineColor: Color,
5058+
?ContourLineSmoothing: float,
5059+
?ContourLineWidth: float,
5060+
?ShowContourLines: bool,
5061+
?ShowContourLabels: bool,
5062+
?ContourColoring: StyleParam.ContourColoring,
5063+
?NContours: int,
5064+
?HistNorm: StyleParam.HistNorm,
5065+
?ContourOpacity: float,
5066+
?ColorBar: ColorBar,
5067+
?ColorScale: StyleParam.Colorscale,
5068+
?ShowScale: bool,
5069+
?UseDefaults: bool
5070+
) =
5071+
5072+
let showContourLines = defaultArg ShowContourLines false
5073+
let pointOpacity = defaultArg PointOpacity 0.3
5074+
5075+
let contourColoring =
5076+
defaultArg ContourColoring StyleParam.ContourColoring.Fill
5077+
5078+
let useDefaults = defaultArg UseDefaults true
5079+
5080+
let contourLineWidth =
5081+
ContourLineWidth |> Option.map (fun v -> if showContourLines then v else 0.) |> Option.defaultValue 0.
5082+
5083+
let marker =
5084+
Marker.init (?Color = PointMarkerColor, ?Symbol = PointMarkerSymbol, ?Size = PointMarkerSize)
5085+
5086+
let pointTrace =
5087+
Trace2D.initScatter (
5088+
Trace2DStyle.Scatter(
5089+
X = x,
5090+
Y = y,
5091+
Mode = StyleParam.Mode.Markers,
5092+
Marker = marker,
5093+
Opacity = pointOpacity
5094+
)
5095+
)
5096+
5097+
let contourLine =
5098+
Plotly.NET.Line.init (
5099+
?Color = ContourLineColor,
5100+
?Smoothing = ContourLineSmoothing,
5101+
Width = contourLineWidth
5102+
)
5103+
5104+
let contours =
5105+
Contours.init (
5106+
ShowLines = showContourLines,
5107+
Coloring = contourColoring,
5108+
?ShowLabels = ShowContourLabels
5109+
)
5110+
5111+
let densityContourTrace =
5112+
Trace2D.initHistogram2DContour (
5113+
Trace2DStyle.Histogram2DContour(
5114+
X = x,
5115+
Y = y,
5116+
Contours = contours,
5117+
Line = contourLine,
5118+
?NContours = NContours,
5119+
?ColorBar = ColorBar,
5120+
?ColorScale = ColorScale,
5121+
?ShowScale = ShowScale,
5122+
?HistNorm = HistNorm,
5123+
?Opacity = ContourOpacity
5124+
)
5125+
)
5126+
5127+
[
5128+
densityContourTrace :> Trace
5129+
pointTrace :> Trace
5130+
]
5131+
|> GenericChart.ofTraceObjects useDefaults

0 commit comments

Comments
 (0)