Dave D.
Dave D.

Reputation: 767

How to save Plotly Offline graph in format png?

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 perfectly generates C:/tmp/test_plot.html file.

import plotly.offline as offline

offline.init_notebook_mode()

offline.plot({'data': [{'y': [4, 2, 3, 4]}], 
               'layout': {'title': 'Test Plot', 
                          'font': dict(family='Comic Sans MS', size=16)}},
             auto_open=False, filename='C:/tmp/test_plot')

How can I save this graph as png instead of html?

Upvotes: 13

Views: 50647

Answers (4)

Birkan
Birkan

Reputation: 151

import plotly
from html2image import Html2Image
import os

if not os.path.exists("./tmp"):
    os.mkdir("./tmp")

fig = dict(data = your_data, layout=your_layout)
plotly.offline.plot(fig, filename='./tmp/look.html',auto_open=False)

hti = Html2Image()
hti.output_path = './tmp/'
with open('tmp/look.html') as f:
    hti.screenshot(f.read(), save_as='ooo.png')

Upvotes: 3

Jon Mease
Jon Mease

Reputation: 339

Quick update: As of plotly.py 3.2.0, it's now possible to programmatically export figures as static images while fully offline.

This was accomplished by integrating the orca project into plotly.py. Check out the announcement post for some more details.

Upvotes: 6

user8365688
user8365688

Reputation:

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()

Upvotes: 2

Anil_M
Anil_M

Reputation: 11473

offline.plot method has image='png and image_filename='image_file_name' attributes to save the file as a png.

offline.plot({'data': [{'y': [4, 2, 3, 4]}], 
              'layout': {'title': 'Test Plot', 
                         'font': dict(family='Comic Sans MS', size=16)}},
             auto_open=True, image = 'png', image_filename='plot_image',
             output_type='file', image_width=800, image_height=600, 
             filename='temp-plot.html', validate=False)

See more details inside offline.py or online at plotly.

However, one caveat is that , since the output image is tied to HTML, it will open in browser and ask for permissions to save the image file. You can turn that off in your browser settings.

enter image description here

Alternately, You may want to look at plotly to matplotlib conversion using plot_mpl.
Following example is from offline.py

from plotly.offline import init_notebook_mode, plot_mpl
    import matplotlib.pyplot as plt

    init_notebook_mode()

    fig = plt.figure()
    x = [10, 15, 20, 25, 30]
    y = [100, 250, 200, 150, 300]
    plt.plot(x, y, "o")

    plot_mpl(fig)
    # If you want to to download an image of the figure as well
    plot_mpl(fig, image='png')

Upvotes: 16

Related Questions