0

I have a dynamic size of plot in plotly, depending on number of bars: enter image description here

I use such parametrization for legend position:

fig.update_layout(
        bargap=0.1,
        barmode='stack',
        yaxis={
            'side': 'right',
        },
        yaxis2={
            'title': 'PSI',
            'anchor': 'free',
            'overlaying': 'y',
            'side': 'left',
        },
        legend={
            'x': 1.1,
        },
        **layout_params,
    )

But there is a problem of different gaps between plot and legend while using different widths:

enter image description here

enter image description here

Is there any way to make fixed gap without relative calculation?

1 Answers1

0

One way is to have a fixed bar width and bar gap and change figure width dynamically according to the resulting number of bars on the final plot. Additionally, you can add to the conditional statement number of bars that has to be exceeded in order to start changing figure size dynamically etc. But the easiest way would be to move the legend itself below the plot ;)

PS Consider using matplotlib and looking at How can I fix the space between a plot and legend so that new text doesn't change the spacing?