Dynamic width of horizontal legend table - Plotly Express

Viewed 249

Is there a way to fix legend table width with graph width? As soon as I zoom in and zoom out (ctrl+scroll) on html file, the only whing, which is resizing is graph. I want legend table to cover 100% of width page, the same as graph and force legend table to "stretch" together with the graph. The graph is in python.

The only thing, which I have found is here, but I belive it is not python:

layout.legend.x = 0 // corresponds to left edge of plot area
layout.legend.x = 1 // corresponds to right edge of plot area

Minimal reproducible example:

import plotly.express as px

df = px.data.gapminder().query("year==2007")
fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent",
    size="pop", size_max=45, log_x=True)

fig.update_layout(legend=dict(
    orientation="h",
    yanchor="bottom",
    y=1.02,
    xanchor="right",
    x=1,
    bgcolor="LightGray",
    bordercolor="Black",
    borderwidth=1,
    tracegroupgap=50
))

filedate = datetime.now().strftime("%d%m%Y_%H%M")
with open(filedate + '_graph.html', 'a') as f:
    f.write(fig.to_html(full_html=False, include_plotlyjs='cdn',config = {'displayModeBar': False}))

enter image description here

0 Answers
Related