Search code examples
pythonexportplotly-python

Plotly export to static image messes up layour


I would like to export as a static image a subplot composed by Sankey plot and a normal bar diagram. When opened as HTML, the plot is fine and it scale reasonably well with the window size.

When I export it to png this is what I get: enter image description here

which is definitely wrong.

I can't share the code right now but my hypothesis is that some objects fall outside the margins so Plotly start to compress them to make them fit. I know that there are options to set the image size but I would like to find a way to set the output image properties programmaticaly also considering the possibility of exporting to PDF,

Do you have any suggestions?

Thank you


Solution

  • I've had this happen to me. You need to define the size of the figure otherwise it gets squeezed. There are two ways I've used before, setting the size in fig.update_layout() or when exporting the image in fig.write_image().

    updating image:

    fig.update_layout(
        autosize=False,
        width=1920,
        height=1080)
    

    exporting image:

    fig.write_image('secret_sankey_plot.pdf', width=1920, height=1080)