fsprojects / IfSharp

F# for Jupyter Notebooks
Other
441 stars 71 forks source link

Questions about plotting with FsLab (Xplot) #219

Closed ykafia closed 5 years ago

ykafia commented 5 years ago

How do you plot FsLab plots inline ? I've tried to read issues everywhere and none explains how to do, how it is implemented or so

ykafia commented 5 years ago

I found an answer by adding this Display Printer :

App.AddDisplayPrinter(fun (x:XPlot.GoogleCharts.GoogleChart) ->
    {
        ContentType = "text/html"
        Data = x.GetHtml()
    }
)

It works with GoogleCharts, haven't tried it yet with Plotly

zyzhu commented 5 years ago

See a sample snippet

#load "Paket.Generated.Refs.fsx"
#load "XPlot.Plotly.fsx"
#load "Deedle.fsx"

open Deedle
open XPlot.Plotly
open System

let fr =
    let x = [DateTime(2001,1,31),1; DateTime(2001,2,28),2; DateTime(2001,3,31),3] |> Series.ofObservations
    let y = [DateTime(2001,1,31),2; DateTime(2001,2,28),3; DateTime(2001,3,31),4] |> Series.ofObservations
    ["s1", x; "s2", y] |> Frame.ofColumns
fr

let PlotlyLineChartFrame title (fr:Frame<DateTime,string>) =
    let data =
      fr.ColumnKeys
      |> Seq.map(fun k ->
        let data = fr.Columns.[k].As<float>()
        let shortDates = data.Keys |> Seq.map(fun x -> x.ToString("yyyy-MM-dd"))
        Scatter(
          x = shortDates,
          y = data.Values,
          name = k,
          mode = "lines"
        )
      )
    let layout = 
      let xaxis = new Xaxis(tickformat = "%b %Y")
      let yaxis = new Yaxis(hoverformat = ".2%")
      let legend = new Legend(xanchor = "left", x=0.15, y = 0.95)
      Layout(title = title, xaxis = xaxis, yaxis = yaxis, legend = legend)
    Chart.Plot(data, layout)
fr |> PlotlyLineChartFrame "Sample"

let data = [-10.0..0.1..10.0] |> Seq.map(fun x -> x, sin(x))
Chart.Line(data)
zyzhu commented 5 years ago

BTW, I only use Deedle and XPlot packages individually. The packages directly from FsLab are not up to date yet.

zyzhu commented 5 years ago

Forgot to mention that you need to run #load "XPlot.Plotly.fsx" first. That file came with the source of IfSharp. Similar file exist for XPlot.GoogleCharts

ykafia commented 5 years ago

Worked with me, thanks ! I had assumed FsLab was up to date

cgravill commented 5 years ago

There are some more examples in the Feature Notebook here: https://github.com/fsprojects/IfSharp/blob/master/FSharp_Jupyter_Notebooks.ipynb

johnathaningle commented 5 years ago

Using @ykafia method for plotting inline, the chart still opens in a new tab. Am I doing something wrong here? image

ykafia commented 5 years ago

Just use the XPlot packages already present with your Ifsharp, no need to download it again or to create a function to display the html!

cgravill commented 5 years ago

The new tab is likely caused by the |> Chart.Show if you delete that it should come up inline.

This should let you use the included version as @ykafia suggests

#load "XPlot.GoogleCharts.Paket.fsx"
#load "XPlot.GoogleCharts.fsx"