plotly / Kaleido

Fast static image export for web-based visualization libraries with zero dependencies
MIT License
375 stars 38 forks source link

Respect a transparent background when exporting to PDF #185

Open Viech opened 2 years ago

Viech commented 2 years ago

The recommended way to produce figures with transparent backround appears to be the following:

fig.update_layout(
    paper_bgcolor="rgba(0,0,0,0)",
    plot_bgcolor="rgba(0,0,0,0)"
)

When I export such a figure using the kaleido backend (version 0.2.1), I obtain a transparent background for SVG images, but not for the PDF format, even though PDF supports transparent backgrounds:

pio.write_image(fig, "output.svg")  # Background is transparent.
pio.write_image(fig, "output.pdf")  # Background is white!

This is using plotly 5.5.0.

empet commented 2 years ago

I also have plotly v5.5.0, but it saves a transparent pdf file of 300dpi, width=5cm (1.9685inches), heigth=3cm (1.1811inches):

pio.write_image(fig, "transparent.pdf", width=300*1.9685, height=300*1.1811,  scale=1) 

transparent.pdf Before setting transparent bg_color, the corresponding png image looks like this: transparent

Viech commented 2 years ago

This indeed removes the grey canvas, but I still see a white background with every viewer I tested (firefox, geeqie, gimp, mupdf, evince, \includegraphics in latex occludes a watermark).

Facenomore23 commented 2 years ago

I also had the same issue as you, I wanted to export as a PDF without background. I could only manage to do it by using the engine "orca". fig.write_image(file, engine= "orca") But this require you to install plenty additional packages and to use a conda environment, which is less convenient than using "Kaleido" (default).

janosh commented 1 year ago

Would be great if someone with background knowledge could chime in to explain how much work adding transparency support to kaleido would be.