Skip to content

Instantly share code, notes, and snippets.

@mh0w
Last active February 6, 2024 14:51
Show Gist options
  • Save mh0w/802bdf07f4a1e8c8322f33afb4011365 to your computer and use it in GitHub Desktop.
Save mh0w/802bdf07f4a1e8c8322f33afb4011365 to your computer and use it in GitHub Desktop.
Create animated plotly express line or scatter chart
# From https://plotly.com/python/sliders/
import plotly.express as px
df = px.data.gapminder()
fig1 = px.scatter(
df,
x="gdpPercap",
y="lifeExp",
animation_frame="year",
animation_group="country",
size="pop",
color="continent",
hover_name="country",
log_x=True,
size_max=55,
range_x=[100, 100000],
range_y=[25, 90],
)
fig2 = px.line(
df,
x="gdpPercap",
y="lifeExp",
animation_frame="year",
animation_group="country",
size="pop",
color="continent",
hover_name="country",
log_x=True,
size_max=55,
range_x=[100, 100000],
range_y=[25, 90],
)
# Optional extra configuration changes
# fig.layout.updatemenus[0].buttons[0].args[1]["frame"]["duration"] = 2500
# fig.layout.updatemenus[0].buttons[0].args[1]["transition"]["duration"] = 100
# fig.layout.updatemenus[0].pad = dict(r=5, t=220)
# fig.layout.sliders[0].pad = dict(t=220)
# fig["layout"].pop("updatemenus") # optional, drop animation buttons
fig1.show("browser")
# SEO terms: animation, animated, animate, slide, slider, sliders, scroll bar,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment