Moving the legend in plotly f#

Viewed 80

I've been trying to move the legend in a plotly plot to the top or the bottom of the plot to maintain uniform sizes of plots with different legnds but I have been unable to do so. Even the plotly documentation has not been very helpful. Does anyone know how to do this?

I tried moving the legend for this example code but I have not been successfull.


let chart = Chart.Scatter(xs,
             [10.; 20.; nan; 15.; 10.; 5.; 15.;nan; 20.; 10.; 10.; 15.; 25.; 20.; 10.], 
             StyleParam.Mode.Lines_Markers, Name="<b>No</b> Gaps")             
             |> GenericChart.mapTrace (Trace2DStyle.Scatter(ConnectGaps = true))
[
    
    chart;
    Chart.Scatter(xs,
             [5.; 15.; nan; 10.; 5.; 0.; 10.; nan; 15.; 5.; 5.; 10.; 20.; 15.; 5.], 
             StyleParam.Mode.Lines_Markers, Name="Gaps")
] |> Chart.combine

Thanks!

2 Answers

This GitHub comment gives some relevant info for Plotly.NET 2.0. In short, you can move the legend to the top of the plot like this:

open Plotly.NET.LayoutObjects

let myLegend = 
    Legend.init(
        Orientation = StyleParam.Orientation.Horizontal,
        Y = 1.1
    )

    ...
    |> Chart.combine
    |> Chart.withLegend(myLegend)

enter image description here

Setting Y to 0.0 instead will put the legend below the chart.

enter image description here

More details on Legend.init can be found here.

There are not examples for all the options, but if you use the search bar on the plotly.net website you can search for the relevant functions that show various options.

You probably want the Chart.withLegendStyle function. You can use the X and Y parameters to move the legend. The function documentation in Plotly.NET's API documentation describes the parameters and options.

Some sample data:

#r "nuget: Plotly.NET"

open Plotly.NET

let xs = [1.0 .. 4.0]
let y1 = xs |> List.map (fun x -> 
    -4.0 + if x = 3.0 then nan else x*1.0)
let y2 = xs |> List.map (fun x -> 
    3.0 + if x = 2.0 then nan else -x*2.0)

To place the legend below you only have to change the legend orientation, because horizontal legends default to being placed below:

[   
    Chart.Scatter(xs,y1, 
        StyleParam.Mode.Lines_Markers, 
        Name="<b>No</b> Gaps")             
    |> GenericChart.mapTrace (Trace2DStyle.Scatter(ConnectGaps = true))
    
    Chart.Scatter(xs,y2, 
                  StyleParam.Mode.Lines_Markers, 
                  Name="Gaps")
]
|> Chart.combine
|> Chart.withLegendStyle(Orientation=StyleParam.Orientation.Horizontal)
|> Chart.show

enter image description here

To place the legend above, you must also change the Y parameter:

[   
    Chart.Scatter(xs,y1, 
        StyleParam.Mode.Lines_Markers, 
        Name="<b>No</b> Gaps")             
    |> GenericChart.mapTrace (Trace2DStyle.Scatter(ConnectGaps = true))
    
    Chart.Scatter(xs,y2, 
                  StyleParam.Mode.Lines_Markers, 
                  Name="Gaps")
]
|> Chart.combine
|> Chart.withLegendStyle(Orientation=StyleParam.Orientation.Horizontal,
                         Y=1.1)

enter image description here

Related