Powered by AppSignal & Oban Pro

Automatically Adjust Margins

35_automatically_adjust_margins.livemd

Automatically Adjust Margins

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

Section

Set automargin: true on a Pie trace to let Plotly automatically expand the figure margins so that outside labels are never clipped.

alias Plotly.{Figure, Pie}

Figure.new()
|> Figure.add_trace(
  Pie.new(
    labels: ["Labrador Retriever", "German Shepherd", "Golden Retriever",
             "French Bulldog", "Bulldog", "Poodle", "Beagle"],
    values: [12, 10, 9, 8, 5, 5, 4],
    textinfo: "label+percent",
    textposition: "outside",
    automargin: true
  )
)
|> Figure.update_layout(
  title: "Dog Popularity — Auto Margin",
  height: 500,
  width: 500,
  margin: %{t: 0, b: 0, l: 0, r: 0},
  showlegend: false
)
|> Plotly.show()