How can I turn on autoplay for a Plotly express animation? (Python)

Viewed 135

there is no documentation on this at all and so I am just putting this out there as these questions on Plotly forums are never answered (although there being a lot of views for these questions, indicating lots of people encounter this problem).

Would appreciate any ideas.

1 Answers

According to Plotly Express documentation, by default, a plot in HTML format will autoplay. So I guess the solution is to save your interactive plot as HTML and then open this saved file.

In Python,

import plotly.express as px
import pandas as pd
import webbrowser

df = pd.DataFrame(dict(
    x=[1, 3, 2, 4],
    y=[1, 2, 3, 4],
    t=[2000, 2001, 2002, 2003]
))
fig = px.scatter(df, x="x", y="y",
                 animation_frame="t",
                 size='x',
                 range_x=[0, 6], range_y=[0, 6]
                 )
# instead of using fig.show() use this:
fig.write_html("test.html")
url = 'test.html'
webbrowser.open(url, new=2) 

Related