Animations in Plotly
Why animations
Animations are useful when you want to show change over time as motion, not just as a static trend line:
- GDP vs life expectancy by year
- Covid cases by month
Under the hood, Plotly builds one frame per value of your animation column, then adds Play/Pause controls and a slider that swap between frames. It’s not “real” motion — it’s the same trick as a flipbook, just automated.
Animated scatter (classic example)
import plotly.express as px
df = px.data.gapminder()
fig = px.scatter(
df,
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
hover_name="country",
log_x=True,
size_max=60,
animation_frame="year",
animation_group="country",
title="Gapminder: GDP vs Life Expectancy (animated)",
)
fig.show()import plotly.express as px
df = px.data.gapminder()
fig = px.scatter(
df,
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
hover_name="country",
log_x=True,
size_max=60,
animation_frame="year",
animation_group="country",
title="Gapminder: GDP vs Life Expectancy (animated)",
)
fig.show()animation_frame="year"animation_frame="year" tells Plotly to build one frame per year; animation_group="country"animation_group="country" tells it which points in different frames are “the same point” so a bubble smoothly moves/resizes instead of popping to a new spot each frame.
Tips
- Animations work best with fewer points (or aggregation) — dozens of moving bubbles read clearly, thousands become a blur.
- Add
animation_groupanimation_groupso Plotly tracks each entity across frames instead of treating every frame as unrelated data. - Consider facets if animation is too visually busy — sometimes seeing all years at once, side by side, communicates the trend faster than watching it play out.
flowchart LR A["DataFrame with a time/category column"] --> B["animation_frame=col
one frame built per unique value"] B --> C["animation_group=id
links the same entity across frames"] C --> D["Play / Pause / slider controls
swap between precomputed frames"]
Next
This wraps the interactive visualization phase — continue to Phase 8: Statistics for Data Analytics to build the analytical foundations behind these charts.
🧪 Try It Yourself
Exercise 1 – Animate Over a Column
Exercise 2 – Track Entities Across Frames
Exercise 3 – Use a Log Axis for Skewed Data
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
