How to save Plotly Offline graph in format png?

后端 未结 3 630
你的背包
你的背包 2020-12-10 04:48

I am using Plotly offline to generate graph in python.

As per the documentation below,

https://plot.ly/python/offline/

Here is my code, which perfec

3条回答
  •  生来不讨喜
    2020-12-10 05:04

    You can automate PhantomJS to save a screenshot with exactly the same width and height as the original image would be when it is downloaded by opening the browser.

    Here is the code:

    import plotly.offline as offline
    from selenium import webdriver
    
    offline.plot({'data': [{'y': [4, 2, 3, 4]}],
                   'layout': {'title': 'Test Plot',
                              'font': dict(size=12)}},
                 image='svg', auto_open=False, image_width=1000, image_height=500)
    
    driver = webdriver.PhantomJS(executable_path="phantomjs.exe")
    driver.set_window_size(1000, 500)
    driver.get('temp-plot.html')
    driver.save_screenshot('my_plot.png')
    
    #Use this, if you want a to embed this .png in a HTML file
    #bs_img = driver.get_screenshot_as_base64()
    

提交回复
热议问题