Plotly : Save graphs as images and html (write_img, write_html)


 We have written articles on how to write graphs, but there will come a time when you will want to save your graphs!  In this article, I will summarize how to save it for that purpose, either as an image or as an html file, retaining the intuitive operation of the graph.


 It can be saved in PNG, JPED, SVG, and PDF formats!  The method is the same for all formats, and can be used with the write_image function!

fig.write_html("filename(include path).xxx")
fig.write_html("sample/test.png") #Save the file as test.png under the sample directory.

 This function corresponds to the savefig function in matplotlib.

 Depending on your version of plotly, you may need to install plotly-orca below, so please check if you get an error when running it.

conda install -c plotly plotly-orca


 One of the advantages of plotly is that you can specify the range of the graph or display only a part of the graph after outputting. If you save the output as an image, you will lose this advantage.  So, I would like to show you how to save as html. By saving as html, you can save the graph while keeping the intuitive operation described above, which is perfect for saving graphs that you don't want to output again, but want to look at again!

 The writing process is almost the same as for write_img!

fig.write_html("filename(include path).html")
fig.write_html("sample/test.html") #Save the file as test.html under the sample directory. 

Sample code

import plotly.graph_objects as go

fig = go.Figure()
    go.Scatter(x=[1, 2, 3], y=[40, 25, 60]),

fig.write_image("test.png") # save as test.png
fig.write_html("test.html") # save as test.html


Copied title and URL