Powered by AppSignal & Oban Pro

Positioning the Legend Outside the Plot

91_positioning_the_legend_outside_the_plot.livemd

Positioning the Legend Outside the Plot

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

Section

Set legend.x to a value greater than 1 (or less than 0) to place the legend outside the plot area. Common positions: right (x: 1.02) or below (y: -0.2).

alias Plotly.{Figure, Scatter}

Figure.new()
|> Figure.add_trace(Scatter.new(x: [1, 2, 3], y: [2, 1, 3], name: "Alpha", mode: "lines"))
|> Figure.add_trace(Scatter.new(x: [1, 2, 3], y: [1, 3, 2], name: "Beta", mode: "lines"))
|> Figure.add_trace(Scatter.new(x: [1, 2, 3], y: [3, 2, 1], name: "Gamma", mode: "lines"))
|> Figure.update_layout(
  title: "Legend Outside the Plot (right)",
  legend: %{x: 1.05, y: 1, xanchor: "left", yanchor: "top"}
)
|> Plotly.show()