How to save Plotly Offline graph in png format?

I am using Plotly offline to create a graph in python.

In accordance with the documentation below

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

Here is my code that perfectly generates the C file: /tmp/test_plot.html.

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 to save this chart as png instead of html?

+5
source share
2 answers

offline.plot has the attributes image='png and image_filename='image_file_name' to save the file as 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 plotly or online plotly .

However, one caveat is that since the output image is HTML bound, it will open in your browser and ask for permission to save the image file. You can disable this in your browser settings.

enter image description here

On the other hand, you might want to take a look at the matplotlib graphic transform using plot_mpl .
The next example is 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') 
+6
source

You can automate PhantomJS to save a screenshot with the same width and height as the original image when it loads, 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() 
+1
source

Source: https://habr.com/ru/post/1258758/


All Articles