plotly / Kaleido

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

Orientation & marker size changes on static export of vector graphics. #187

Open drscotthawley opened 1 year ago

drscotthawley commented 1 year ago

Following the Plotly documentation for static image export: https://plotly.com/python/static-image-export/

When I try to save the figure using the suggested call to fig.write_image, it ignores my rotation and zoom adjustments and saves with some default orientation and a much larger marker size. The documentation says nothing about this, or how to fix it.

As an example to reproduce this problem, let's take the 3D Scatter plots documentation example (I'll add a light height to it too):

import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
              color='species', height=500)
fig.show()

If i execute this in a Jupyter cell, I can rotate the graph, press the "Download as PNG" button, and get exactly what I see. For example, this image: fig1

But if I then run the recommended export code to save a PDF,

fig.write_image("fig1.pdf")

and then view the PDF file, I see something different: the orientation resets and the marker size is huge (screenshot): Screen Shot 2023-04-02 at 5 41 51 PM

Given that the docs don't address this, I'm wondering if it's a bug. Otherwise, there's quite an omission in the documentation.

Either way, my question is how can we export vector images that preserve orientation & marker size? Thanks.

(Workaround: Currently I'm trying to "guess" my camera controls to set them programmatically before the save, but it's a slow iterative process. If there's a way to query fig.layout.scene.camera.whatever to then go back to fix the camera for save, please let me know. And add it to the docs! Marker size is the same thing -- I know what markers I see, but have to keep re-saving and checking the size because it's a different size in the PDF. :-( )

Version info:

$ pip list | grep 'plotly\|kaleido'
kaleido                   0.2.1
plotly                    5.14.0
$ python --version
Python 3.10.6
drscotthawley commented 1 year ago

Update: I do have Kaleido installed but hadn't invoked the export settings. If I add the code

import plotly.io as pio
pio.kaleido.scope.default_format = "pdf"

and run it before the fig.write_image call....nothing different happens. :-( The PDF is still "wrong".

gvwilson commented 4 months ago

Thanks for your interest in Kaleido. We are currently working on an overhaul that might address your issue - we hope to have news in a few weeks and will post an update then. Thanks - @gvwilson