Search code examples
pythonplotly

How to save a plotly graph keeping its interactivity


I finished my plotly scatterplot and I'm trying to save it to be able to present it in a presentation, for instance power point (tho it could be pdf format as well). If I save it, it is saved in png format, as a picture, and it loses the interactivity, which is the fundamental part of the graph. How can I save it with the interactive condition? 1


Solution

  • Of course is not possible to export an interactive PNG, but you can export the graph in html using fig.write_html.

    As an example:

    import plotly.express as px
    fig =px.scatter(x=range(20), y=range(20))
    fig.write_html("file.html")
    

    This will create a file in the same folder of your code called "file.html".

    If you open this file with a browser, it will result in: Generated HTML

    You can open this file as any other HTML file, and you can interact with the graph (than you can integrate it wherever you want).

    Here you can find the documentation: https://plotly.com/python/interactive-html-export/ where you can find the alternative with Dash too.