F# Charting


F# Charting: BoxPlot Charts

Summary: This example shows how to create boxplot diagrams in F#. It looks at how to create a single boxplot from six statistics about an observation set as well as how to automatically create boxplots from observations.

When creating boxplot charts, it is possible to use either six statistics (Lower whisker, Upper whisker, Lower box, Upper box, Average, Median) that define the boxplot data, or to infer these statistics from a set of values and let the library generate the boxplot diagram automatically.

A boxplot diagram shows six basic statistics about a set of observations. It displays the dataset minimum and maximum, the upper and lower quartiles, the average value, and the median. When the F# script calculates these six statistics, the values can be passed to the method Chart.BoxPlotFromStatistics as a list of tuples to draw multiple boxplots.

1: 
2: 
3: 
4: 
5: 
6: 
// On Mac OSX use FSharp.Charting.Gtk.fsx
#I "packages/FSharp.Charting"
#load "FSharp.Charting.fsx"

open FSharp.Charting
open System
1: 
2: 
3: 
Chart.BoxPlotFromStatistics( 
    [ ("Result A", -12.7, 11.6, -8.3, 6.4, 0.0, 0.0);
      ("Result B", -6.7, 11.6, -5.0, 5.4, 0.0, 0.0) ])

Chart

Here is the same box plot with dates used as labels. These must be explicitly formatted as strings.

1: 
2: 
3: 
4: 
Chart.BoxPlotFromStatistics(
    [ (DateTime.Today.ToShortDateString()             , -12.7, 11.6, -8.3, 6.4, 0.0, 0.0);
      (DateTime.Today.AddDays(1.0).ToShortDateString(), -6.7, 11.6, -5.0, 5.4, 0.0, 0.0) ],
    ShowMedian = false, ShowAverage = false)

Chart

This snippet calls the Chart.BoxPlotFromStatistics method with a list containing
(Label, Lower whisker, Upper whisker, Lower box, Upper box, Average, Median) pairs. The call uses the value 0.0 as a placeholder for the last two statistics. The lines in the boxplot diagram have to be hidden explicitly by setting BoxPlotShowMedian and BoxPlotShowAverage to false.

Another alternative when creating a boxplot is to let the charting library calculate the boxplot statistics automatically. To do that, the Chart.BoxPlot method can be called with a collection of (xLabel, yValues) tuples as an argument. Each entry is a label plus a set of observations. The statistics are automatically computed from the values in the observations.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
let date n = DateTime.Today.AddDays(float n).ToShortDateString()
let rnd = new System.Random()

let threeSyntheticDataSets = 
    [ (date 0, [| for i in 0 .. 20 -> float (rnd.Next 20) |])
      (date 1, [| for i in 0 .. 20 -> float (rnd.Next 15 + 2) |])
      (date 2, [| for i in 0 .. 20 -> float (rnd.Next 10 + 5) |]) ]
1: 
2: 
3: 
4: 
Chart.BoxPlotFromData
  ( threeSyntheticDataSets,
    ShowUnusualValues = true, ShowMedian = false,
    ShowAverage = false, WhiskerPercentile = 10)

Chart

The example above demonstrates how to calculate boxplot diagrams automatically from (randomly generated) data. It is also possible to set several custom properties to configure the boxplot diagram. When BoxPlotShowUnusualValues is true, the boxplot displays unusual values using points, as shown in Figure 1.

More information about working with financial data and how to download stock prices from the Yahoo Finance portal using F# can be found in Try F#.

Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
namespace FSharp.Charting
namespace System
type Chart =
  static member Area : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Area : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Bar : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Bar : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
  static member BoxPlotFromData : data:seq<#key * #seq<'c>> * ?Name:string * ?Title:string * ?Color:Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> GenericChart (requires 'c :> value)
  static member BoxPlotFromStatistics : data:seq<#key * #value * #value * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> GenericChart
  static member Bubble : data:seq<#value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?BubbleMaxSize:int * ?BubbleMinSize:int * ?BubbleScaleMax:float * ?BubbleScaleMin:float * ?UseSizeForLabel:bool -> GenericChart
  static member Bubble : data:seq<#key * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?BubbleMaxSize:int * ?BubbleMinSize:int * ?BubbleScaleMax:float * ?BubbleScaleMin:float * ?UseSizeForLabel:bool -> GenericChart
  static member Candlestick : data:seq<#value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> CandlestickChart
  static member Candlestick : data:seq<#key * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> CandlestickChart
  ...

Full name: FSharp.Charting.Chart
static member Chart.BoxPlotFromStatistics : data:seq<#key * #value * #value * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Drawing.Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> ChartTypes.GenericChart
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
DateTime()
   (+0 other overloads)
DateTime(ticks: int64) : unit
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
   (+0 other overloads)
property DateTime.Today: DateTime
DateTime.ToShortDateString() : string
DateTime.AddDays(value: float) : DateTime
val date : n:int -> string

Full name: BoxPlotCharts.date
val n : int
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val rnd : Random

Full name: BoxPlotCharts.rnd
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
Random() : unit
Random(Seed: int) : unit
val threeSyntheticDataSets : (string * float []) list

Full name: BoxPlotCharts.threeSyntheticDataSets
val i : int
Random.Next() : int
Random.Next(maxValue: int) : int
Random.Next(minValue: int, maxValue: int) : int
static member Chart.BoxPlotFromData : data:seq<#key * #seq<'c>> * ?Name:string * ?Title:string * ?Color:Drawing.Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> ChartTypes.GenericChart (requires 'c :> value)
Fork me on GitHub