简体   繁体   中英

Unable to export plotly images to png with Kaleido

I have never used plotly before, and I have been trying to export a sample image code to png. I have installed plotly and kaleido and ran the code on python 3, but nothing happens.

#Here is the code:

import plotly.express as px
import numpy as np
    
# RGB Data as numpy array
img_rgb = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]],], dtype=np.uint8)
  
fig = px.imshow(img_rgb)
fig.show()

fig.write_image("fig.png")

According to the plotly documentation, that should do the trick, but I can't get a png image. It shows no errors, no warnings... it's like python is stuck in an infinite loop. I can't even stop the terminal from running.

I'm using python 3.9.6 64-bit
I have tried plotly latest version (5.3.1) and an older version (4.14.3), but I've got the same problem.
python -m pip install plotly

kaleido is the latest version (0.2.1), but I have tried version (0.2.0) too.
python -m pip install -U kaleido

What could I be doing wrong?

UPDATE: I have tried the same process in another computer, and it worked ok. Any ideas why the first computer won't run the code?

I had the same issue. tried everything. nothing worked. then finally installed orca. and.., It worked.!! I would suggest installing Orca for static image write when using Plotly, if nothing works.

fig.write_image('fig1.png', engine='orca')

I'm facing the same issues as above. But stranger, it works on my machine but not on the user's machine.

  • We both have Windows Enterprise 10, same version same updates, ... (distributed by Software Center)
  • We have identical Python environments (same conda env, distributed by Software Center to all the data scientists, users have no access to this directory to make updates)
  • We run the exact same test code from an Anaconda prompt in the same conda env
  • We cannot simply downgrade the version of kaleido because of this type of distribution
  • For both, it works with ORCA (so it is kaleido posing the problem)

The only difference so far I can spot, is our machine itself. I run on an Intel, he runs on an AMD processor.

Well, there's actually a second. I run my Windows in English, he in Dutch.

The people above who don't have this working with 1.2, it could be a suggestion to check your processor. On my Intel it works, on his AMD it doesn't.

And, for those that don't have it working: are you running on Windows in English version?

Provided you have the kaleido installed, you should specify the engine as kaleido in the object write_image .

I could recreate the produce the following with the solution:

import plotly.express as px
import numpy as np

    
# RGB Data as numpy array
img_rgb = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]],], dtype=np.uint8)
  
fig = px.imshow(img_rgb)
fig.show()

fig.write_image('fig.png', engine='kaleido')

Resultant Image: 图片

Resultant path of image saved: image_saved_to_path

I run Windows 10 Enterprise and this worked for me:

Downgrade Kaleido to version 0.1.0post1 (which is the latest in 0.1.*):

pip install --upgrade "kaleido==0.1.*"

Then image creation started working. Also see:

https://stackoverflow.com/a/71621135/660259

Funny thing? kaleido v0.2.1 works fine on windows server 2019, but not on my work-computer Windows 10 Enterprise

I had the same problem.

Following the discussion here https://github.com/plotly/Kaleido/issues/36 I disabled mathjax

import plotly.io as pio
pio.kaleido.scope.mathjax = None

and voila! plots started to appear.

Apparently this should be fixed from plotly v. 5 onwards, but it's not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM