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()animation_frame="year" tells Plotly to build one frame per year; 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_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"]
Check your understanding
What does animation_frame="year" do in the gapminder example?
Plotly builds one frame per value of the animation column and adds Play/Pause controls plus a slider.
Show answer
B — Builds one frame per year with play controls and a slider — Plotly builds one frame per value of the animation column and adds Play/Pause controls plus a slider.
What is animation_group="country" for?
It lets a bubble move smoothly between frames instead of being treated as unrelated data.
Show answer
A — Telling Plotly which points across frames are the same entity — It lets a bubble move smoothly between frames instead of being treated as unrelated data.
In the gapminder animated scatter, what does log_x=True apply to?
x is gdpPercap, which spans a huge range, so a log scale spreads the points out.
Show answer
D — GDP per capita on the x axis — x is gdpPercap, which spans a huge range, so a log scale spreads the points out.
When might facets be better than an animation?
Seeing all years side by side can communicate the trend faster and allows precise comparison.
Show answer
A — When the animation is too busy and you want all years visible at once — Seeing all years side by side can communicate the trend faster and allows precise comparison.
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
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Track Entities Across Frames
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Use a Log Axis for Skewed Data
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.