// can't yet format YamlFrontmatter (["title: Axis styling"; "category: Chart Layout"; "categoryindex: 2"; "index: 1"], Some { StartLine = 2 StartColumn = 0 EndLine = 6 EndColumn = 8 }) to pynb markdown

In [ ]:
#r "nuget: Plotly.NET, 2.0.0-preview.7"
#r "nuget: Plotly.NET.Interactive, 2.0.0-preview.7"

Axis styling

BinderScriptNotebook

Summary: This example shows how to style chart axes in F#.

let's first create some data for the purpose of creating example charts:

In [3]:
open Plotly.NET 
  
let x = [1.; 2.; 3.; 4.; 5.; 6.; 7.; 8.; 9.; 10.; ]
let y = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
let y' = y |> List.map (fun y -> y * 2.) |> List.rev

Single axis styling

To style a specific axis of a plot, use the respective Chart.with*_AxisStyle function:

In [4]:
let plot1 =
    Chart.Point(x,y)
    |> Chart.withXAxisStyle ("X axis title", MinMax = (-1.,10.))
    |> Chart.withYAxisStyle ("Y axis title", MinMax = (-1.,10.))
In [ ]:
plot1
Out[ ]:

for even more fine-grained control, initialize a new axis and replace the old one of the plot with ````. The following example creates two mirrored axes with inside ticks, one of them with a log scale:

In [6]:
let mirroredXAxis =
    Axis.LinearAxis.init(
        Title = Title.init(Text="Mirrored axis"),
        ShowLine = true,
        Mirror = StyleParam.Mirror.AllTicks,
        ShowGrid = false,
        Ticks = StyleParam.TickOptions.Inside
    )

let mirroredLogYAxis = 
    Axis.LinearAxis.init(
        Title = Title.init(Text="Log axis"),
        AxisType = StyleParam.AxisType.Log,
        ShowLine = true,
        Mirror = StyleParam.Mirror.AllTicks,
        ShowGrid = false,
        Ticks = StyleParam.TickOptions.Inside
    )

let plot2 =
    Chart.Point(x,y)
    |> Chart.withXAxis mirroredXAxis
    |> Chart.withYAxis mirroredLogYAxis
In [ ]:
plot2
Out[ ]:

Multiple axes

Assign different axis anchors to subplots to map them to different axes.

Multiple axes on different sides of the chart

The following example first creates a multichart containing two plots with different axis anchors. Subsequently, multiple axes with the respective anchors are added to the plot. Note that the same can be done as above, defining axes beforehand.

In [8]:
let anchoredAt1 =
    Chart.Line (x,y,Name="anchor 1")
        |> Chart.withAxisAnchor(Y=1)

let anchoredAt2 =
     Chart.Line (x,y',Name="anchor 2")
        |> Chart.withAxisAnchor(Y=2)

let twoXAxes1 = 
    [
       anchoredAt1
       anchoredAt2
    ]
    |> Chart.combine
    |> Chart.withYAxisStyle(
        "axis 1",
        Side=StyleParam.Side.Left,
        Id=StyleParam.SubPlotId.YAxis 1
    )
    |> Chart.withYAxisStyle(
        "axis2",
        Side=StyleParam.Side.Right,
        Id=StyleParam.SubPlotId.YAxis 2,
        Overlaying=StyleParam.LinearAxisId.Y 1
    )
        
In [ ]:
twoXAxes1
Out[ ]:

Multiple axes on the same side of the chart

Analogous to above, but move the whole plot to the right by adjusting its domain, and then add a second axis to the left:

In [10]:
let twoXAxes2 =
    [
        anchoredAt1
        anchoredAt2
    ]
    |> Chart.combine
    |> Chart.withYAxisStyle(
            "first y-axis",
            ShowLine=true
            )
    |> Chart.withXAxisStyle(
        "x-axis",
        Domain=(0.3, 1.0) // moves the first axis and the whole plot to the right
    ) 
    |> Chart.withYAxisStyle(
        "second y-axis",
        Side=StyleParam.Side.Left,
        Id=StyleParam.SubPlotId.YAxis 2,
        Overlaying=StyleParam.LinearAxisId.Y 1,
        Position=0.10, // position the axis beteen the leftmost edge and the firt axis at 0.3
        //Anchor=StyleParam.AxisAnchorId.Free,
        ShowLine=true
    )
In [ ]:
twoXAxes2
Out[ ]: