Powered by AppSignal & Oban Pro

Subplot Annotations

126_subplot_annotations.livemd

Subplot Annotations

Mix.install([
  {:plotly_ex, "~> 0.3"},
  {:kino, "~> 0.18"}
])

Section

To annotate a specific subplot, set xref and yref to the subplot's axis names: "x2" / "y2" for the second subplot, "x3" / "y3" for the third, etc. Use "paper" references for annotations that span the whole figure.

alias Plotly.{Figure, Scatter}

Figure.new()
|> Figure.add_trace(Scatter.new(x: [1, 2, 3], y: [4, 5, 6], mode: "lines", name: "Subplot 1"))
|> Figure.add_trace(
  Scatter.new(
    x: [20, 30, 40],
    y: [50, 60, 70],
    mode: "lines",
    name: "Subplot 2",
    xaxis: "x2",
    yaxis: "y2"
  )
)
|> Figure.update_layout(
  title: "Subplot Annotations",
  xaxis: %{domain: [0, 0.45]},
  xaxis2: %{domain: [0.55, 1]},
  yaxis2: %{anchor: "x2"},
  annotations: [
    %{
      x: 2,
      y: 5,
      xref: "x",
      yref: "y",
      text: "Left subplot",
      showarrow: true,
      arrowhead: 2,
      ax: 0,
      ay: -30
    },
    %{
      x: 30,
      y: 60,
      xref: "x2",
      yref: "y2",
      text: "Right subplot",
      showarrow: true,
      arrowhead: 2,
      ax: 0,
      ay: -30
    }
  ]
)
|> Plotly.show()