Powered by AppSignal & Oban Pro

Positioning the Legend Inside the Plot

90_positioning_the_legend_inside_the_plot.livemd

Positioning the Legend Inside the Plot

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

Section

Use layout.legend with x, y, xanchor, and yanchor (paper coords, 0–1) to place the legend inside the plot area.

alias Plotly.{Figure, Scatter}

Figure.new()
|> Figure.add_trace(Scatter.new(x: [1, 2, 3], y: [2, 1, 3], name: "A", mode: "lines"))
|> Figure.add_trace(Scatter.new(x: [1, 2, 3], y: [3, 2, 1], name: "B", mode: "lines"))
|> Figure.update_layout(
  title: "Legend Inside the Plot (top-right)",
  legend: %{x: 1, xanchor: "right", y: 1}
)
|> Plotly.show()