Auto plot offset for large figure titles

import plotly.express as px

df = px.data.gapminder().query("continent == 'Oceania'")
fig = px.line(df, x="year", y="gdpPercap", color="country")

fig = fig.update_layout(
    title=dict(text="GDP-per-capita", font=dict(size=100), automargin=True, yref='paper', pad_t=30),
    autosize=True
)

fig.show()

The large title either overlaps with the plot area or is pushed outside the figure. How to ensure the title is always shown whole without overlaps and without setting margins manually? I tried playing with automargin, yref=โ€œpaperโ€ or โ€œcontainterโ€, autosize True or False but nothing did the trick.

Hi @Madrigallo ,

I try to play with automargin, yref and autosize but end up can not get the solution.

So I try to set yref="container" and unfortunately, must set top margin layout.
You can set top margin layout by adding default top margin fig.layout.margin.t and your font size.

import plotly.express as px

df = px.data.gapminder().query("continent == 'Oceania'")
fig = px.line(df, x="year", y="gdpPercap", color="country")

font_size = 100
fig.update_layout(
    title=dict(text="GDP-per-capita", font=dict(size=font_size), automargin=True, yref='container', pad_t=30),
)
margin_t = fig.layout.margin.t + font_size

fig.update_layout(margin=dict(t=margin_t))
fig.show()

I know it end up to set top margin manually, but hope this can help you.

Thanks for the idea, this actually might work as Iโ€™d like. Iโ€™m just surprised thereโ€™s not a more straightforward way.