Skip to content

Commit c40ef56

Browse files
committed
Add violin plot
1 parent 1bc6454 commit c40ef56

File tree

7 files changed

+132
-17
lines changed

7 files changed

+132
-17
lines changed

FSharp.Plotly.sln

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 15
3-
VisualStudioVersion = 15.0.27004.2010
3+
VisualStudioVersion = 15.0.27130.2020
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".paket", ".paket", "{63297B98-5CED-492C-A5B7-A5B4F73CF142}"
66
ProjectSection(SolutionItems) = preProject
@@ -50,6 +50,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "content", "content", "{8E6D
5050
docs\content\polar-charts.fsx = docs\content\polar-charts.fsx
5151
docs\content\range-plots.fsx = docs\content\range-plots.fsx
5252
docs\content\shapes.fsx = docs\content\shapes.fsx
53+
docs\content\violin-plots.fsx = docs\content\violin-plots.fsx
5354
docs\content\windrose-charts.fsx = docs\content\windrose-charts.fsx
5455
EndProjectSection
5556
EndProject

docs/content/box-plots.fsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Outliers may be plotted as individual points.
1414
*)
1515

1616
open FSharp.Plotly
17+
open FSharp.Plotly.StyleParam
1718

1819
let y = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
1920
let x = ["bin1";"bin2";"bin1";"bin2";"bin1";"bin2";"bin1";"bin1";"bin2";"bin1"]
@@ -22,6 +23,7 @@ let x = ["bin1";"bin2";"bin1";"bin2";"bin1";"bin2";"bin1";"bin1";"bin2";"bin1"]
2223
Chart.BoxPlot(x,y,Jitter=0.1,Boxpoints=StyleParam.Boxpoints.All)
2324
(*** include-it:box1 ***)
2425

26+
2527
(**
2628
By swapping x and y plus using `StyleParam.Orientation.Horizontal` we can flip the chart horizontaly.
2729
*)
@@ -46,5 +48,3 @@ let y' = [2.; 1.5; 5.; 1.5; 2.; 2.5; 2.1; 2.5; 1.5; 1.;2.; 1.5; 5.; 1.5; 3.; 2.
4648
(*** include-it:box3 ***)
4749

4850

49-
50-

docs/content/violin-plots.fsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(*** hide ***)
2+
#r "../../bin/Newtonsoft.Json.dll"
3+
#r "../../bin/FSharp.Plotly.dll"
4+
5+
(**
6+
# FSharp.Plotly: BoxPlot Charts
7+
8+
*Summary:* This example shows how to create boxplot charts in F#.
9+
10+
A box plot or boxplot is a convenient way of graphically depicting groups of numerical data through their quartiles.
11+
Box plots may also have lines extending vertically from the boxes (whiskers) indicating variability outside the upper
12+
and lower quartiles, hence the terms box-and-whisker plot and box-and-whisker diagram.
13+
Outliers may be plotted as individual points.
14+
*)
15+
16+
open FSharp.Plotly
17+
open FSharp.Plotly.StyleParam
18+
19+
let y = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
20+
let x = ["bin1";"bin2";"bin1";"bin2";"bin1";"bin2";"bin1";"bin1";"bin2";"bin1"]
21+
22+
23+
(*** define-output:violin1 ***)
24+
Chart.Violin (x,y,Points=StyleParam.Jitterpoints.All)
25+
(*** include-it:violin1 ***)
26+
27+
28+
29+
(**
30+
By swapping x and y plus using `StyleParam.Orientation.Horizontal` we can flip the chart horizontaly.
31+
*)
32+
(*** define-output:violin2 ***)
33+
Chart.Violin (y,x,Jitter=0.1,Points=StyleParam.Jitterpoints.All,Orientation=StyleParam.Orientation.Horizontal)
34+
(*** include-it:violin2 ***)
35+
36+
37+
38+
(**
39+
You can also produce a boxplot using the `Chart.Combine` syntax.
40+
*)
41+
42+
let y' = [2.; 1.5; 5.; 1.5; 2.; 2.5; 2.1; 2.5; 1.5; 1.;2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
43+
44+
(*** define-output:violin3 ***)
45+
[
46+
Chart.Violin ("y" ,y,Name="bin1",Jitter=0.1,Points=StyleParam.Jitterpoints.All);
47+
Chart.Violin ("y'",y',Name="bin2",Jitter=0.1,Points=StyleParam.Jitterpoints.All);
48+
]
49+
|> Chart.Combine
50+
(*** include-it:violin3 ***)
51+
52+

docs/tools/templates/template.cshtml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,39 @@
3838
<div class="span3">
3939
<img src="@Root/img/logo.png" alt="FSharp.Plotly" style="width:150px;margin:10px" />
4040
<ul class="nav nav-list" id="menu" style="margin-top: 20px;">
41-
<li class="nav-header">@Properties["project-name"]</li>
41+
<li class="nav-header">@Properties["project-name"]</li>
4242
<li><a href="@Root/index.html">Home page</a></li>
43-
<li class="divider"></li>
43+
<li class="divider"></li>
4444
<li><a href="@Root/getting-started.html">Getting started</a></li>
45-
<li class="nav-header">Plotly Charts</li>
45+
<li class="nav-header">Plotly Charts</li>
4646
<li><a href="@Root/area-plots.html">Area Plots</a></li>
47-
<li><a href="@Root/bar-charts.html">Bar</a></li>
47+
<li><a href="@Root/bar-charts.html">Bar</a></li>
4848
<li><a href="@Root/box-plots.html">Box Plots</a></li>
49+
<li><a href="@Root/violin-plots.html">Violin Plots</a></li>
4950
<li><a href="@Root/bubble-charts.html">Bubble Charts</a></li>
5051
<li><a href="@Root/contour-plots.html">Contour Plots</a></li>
5152
<li><a href="@Root/heatmaps.html">Heatmaps</a></li>
5253
<li><a href="@Root/histograms.html">Histograms</a></li>
5354
<li><a href="@Root/2d-histograms.html">2D Histograms</a></li>
5455
<li><a href="@Root/line-scatter-plots.html">Line and Scatter Plots</a></li>
55-
<li><a href="@Root/parallel-coords.html">Parallel coords Plots</a></li>
56+
<li><a href="@Root/parallel-coords.html">Parallel coords Plots</a></li>
5657
<li><a href="@Root/pie-daughnut-charts.html">Pie and Doughnut Charts</a></li>
5758
<li><a href="@Root/polar-charts.html">Polar Charts</a></li>
5859
<li><a href="@Root/windrose-charts.html">Windrose Charts</a></li>
59-
<li><a href="@Root/range-plots.html">Range Plots</a></li>
60-
<li class="nav-header">Plotly 3d-Charts</li>
61-
<li><a href="@Root/3d-scatter-plots.html">3D Scatter Plots</a></li>
62-
<li><a href="@Root/3d-line-plots.html">3D Line Plots</a></li>
60+
<li><a href="@Root/range-plots.html">Range Plots</a></li>
61+
<li class="nav-header">Plotly 3d-Charts</li>
62+
<li><a href="@Root/3d-scatter-plots.html">3D Scatter Plots</a></li>
63+
<li><a href="@Root/3d-line-plots.html">3D Line Plots</a></li>
6364
<li><a href="@Root/3d-surface-plots.html">3D Surface Plots</a></li>
6465
<li><a href="@Root/3d-mesh-plots.html">3D Mesh Plots</a></li>
65-
<li class="nav-header">Styling</li>
66+
<li class="nav-header">Styling</li>
6667
<li><a href="@Root/multiple-charts.html">Multiple Charts</a></li>
67-
<li><a href="@Root/errorbars.html">Error bars</a></li>
68+
<li><a href="@Root/errorbars.html">Error bars</a></li>
6869
<li><a href="@Root/shapes.html">Shapes</a></li>
69-
<li class="nav-header">Plotly WPF</li>
70-
<li><a href="@Root/plotly-wpf.html">Using Plotly PopUp window</a></li>
70+
<li class="nav-header">Plotly WPF</li>
71+
<li><a href="@Root/plotly-wpf.html">Using Plotly PopUp window</a></li>
7172

72-
<li class="nav-header">Documentation</li>
73+
<li class="nav-header">Documentation</li>
7374
<li><a href="@Root/reference/index.html">API Reference</a></li>
7475
</ul>
7576
</div>

src/FSharp.Plotly/Chart.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,21 @@ type Chart =
325325
Chart.BoxPlot(x, y, ?Name=Name,?Showlegend=Showlegend,?Color=Color,?Fillcolor=Fillcolor,?Opacity=Opacity,?Whiskerwidth=Whiskerwidth,?Boxpoints=Boxpoints,?Boxmean=Boxmean,?Jitter=Jitter,?Pointpos=Pointpos,?Orientation=Orientation)
326326

327327

328+
/// Displays the distribution of data based on the five number summary: minimum, first quartile, median, third quartile, and maximum.
329+
static member Violin(?x,?y,?Name,?Showlegend,?Color,?Fillcolor,?Opacity,?Points,?Jitter,?Pointpos,?Orientation) =
330+
Trace.initViolin (TraceStyle.Violin(?X=x, ?Y = y,?Points=Points,
331+
?Jitter=Jitter,?Pointpos=Pointpos,?Orientation=Orientation,?Fillcolor=Fillcolor) )
332+
|> TraceStyle.TraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
333+
|> TraceStyle.Marker(?Color=Color)
334+
|> GenericChart.ofTraceObject
335+
336+
337+
/// Displays the distribution of data based on the five number summary: minimum, first quartile, median, third quartile, and maximum.
338+
static member Violin(xy,?Name,?Showlegend,?Color,?Fillcolor,?Opacity,?Points,?Jitter,?Pointpos,?Orientation) =
339+
let x,y = Seq.unzip xy
340+
Chart.Violin(x, y, ?Name=Name,?Showlegend=Showlegend,?Color=Color,?Fillcolor=Fillcolor,?Opacity=Opacity,?Points=Points,?Jitter=Jitter,?Pointpos=Pointpos,?Orientation=Orientation)
341+
342+
328343
/// Shows a graphical representation of a 3-dimensional surface by plotting constant z slices, called contours, on a 2-dimensional format.
329344
/// That is, given a value for z, lines are drawn for connecting the (x,y) coordinates where that z value occurs.
330345
static member Heatmap(data:seq<#seq<#IConvertible>>,?ColNames,?RowNames,?Name,?Showlegend,?Opacity,?Colorscale,?Showscale,?Xgap,?Ygap,?zSmooth,?Colorbar) =

src/FSharp.Plotly/StyleParams.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ module StyleParam =
210210
| False -> box false
211211

212212

213+
type Jitterpoints = Boxpoints
214+
215+
216+
213217
type BoxMean =
214218
| True
215219
| False

src/FSharp.Plotly/Trace.fs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ module Trace =
2626
let initBoxPlot (applyStyle:Trace->Trace) =
2727
Trace("box") |> applyStyle
2828

29+
/// Init trace for box plot
30+
let initViolin (applyStyle:Trace->Trace) =
31+
Trace("violin") |> applyStyle
32+
2933
/// Init trace for pie chart
3034
let initPie (applyStyle:Trace->Trace) =
3135
Trace("pie") |> applyStyle
@@ -424,6 +428,44 @@ module Trace =
424428
)
425429

426430

431+
// Applies the styles of violin plot plot to TraceObjects
432+
static member Violin
433+
(
434+
?Y,
435+
?X,
436+
?X0,
437+
?Y0,
438+
?Points,
439+
?Jitter,
440+
?Pointpos,
441+
?Orientation,
442+
?Fillcolor,
443+
?xAxis,
444+
?yAxis,
445+
?Ysrc,
446+
?Xsrc
447+
448+
) =
449+
(fun (boxPlot:('T :> Trace)) ->
450+
451+
Y |> DynObj.setValueOpt boxPlot "y"
452+
X |> DynObj.setValueOpt boxPlot "x"
453+
X0 |> DynObj.setValueOpt boxPlot "x0"
454+
Y0 |> DynObj.setValueOpt boxPlot "y0"
455+
Points |> DynObj.setValueOptBy boxPlot "points" StyleParam.Jitterpoints.convert
456+
Jitter |> DynObj.setValueOpt boxPlot "jitter"
457+
Pointpos |> DynObj.setValueOpt boxPlot "pointpos"
458+
Orientation |> DynObj.setValueOptBy boxPlot "orientation" StyleParam.Orientation.convert
459+
Fillcolor |> DynObj.setValueOpt boxPlot "fillcolor"
460+
xAxis |> DynObj.setValueOpt boxPlot "xaxis"
461+
yAxis |> DynObj.setValueOpt boxPlot "yaxis"
462+
Ysrc |> DynObj.setValueOpt boxPlot "ysrc"
463+
Xsrc |> DynObj.setValueOpt boxPlot "xsrc"
464+
465+
// out ->
466+
boxPlot
467+
)
468+
427469

428470

429471
// Applies the styles of heatmap to TraceObjects

0 commit comments

Comments
 (0)